re module methods: flags(), pattern()

X

Xah Lee

Python re module has methods flags and pattern. How to use these
exactly?

e.g. i tried

print patternObj.flags()

and the error is some "int object is not callable".

newpattern=re.compile(ur'\w+',patternObj.flags())

also bad.

similar error for patternObj.pattern(). (and i suppose the same for
groupindex() )

thanks.

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
R

Reinhold Birkenfeld

Xah said:
Python re module has methods flags and pattern. How to use these
exactly?

e.g. i tried

print patternObj.flags()

and the error is some "int object is not callable".

Where do you read "methods"?

Reinhold
 
K

Kay Schluehr

Xah said:
Python re module has methods flags and pattern. How to use these
exactly?
From the Python 2.3 documentation, section 2.4.2

flags

The flags argument used when the RE object was compiled, or 0 if no
flags were provided.


groupindex

A dictionary mapping any symbolic group names defined by (?P<id>) to
group numbers. The dictionary is empty if no symbolic groups were used
in the pattern.

pattern

The pattern string from which the RE object was compiled.


Comments derived from the docs and error messages:
e.g. i tried

print patternObj.flags()

and the error is some "int object is not callable".

=> flags is an int object
newpattern=re.compile(ur'\w+',patternObj.flags())

also bad.

=> flags is still an int object

similar error for patternObj.pattern().

=> pattern is a string
(and i suppose the same for
groupindex() )

=> groupindex is a dict
thanks.

Xah
(e-mail address removed)
∑ http://xahlee.org/

The documentation is not that bad that one should not read it at all ;)


If You wrestle with regexps I can recommend an old but still usefull
little wrapper I found eventually on the "Vaults of Parnassus" which is
called reverb.py

Ciao,
Kay
 
G

Guest

A flag is just an int. From the re doc, you can see
there is a ignorecase flag:

"
I
IGNORECASE
Perform case-insensitive matching; expressions like [A-Z] will
match lowercase letters, too. This is not affected by the current locale.
"

Using the ignorecase flag:
'heLLo'

As the doc says, you can combine several flags with the '|' operator:
r = re.compile("your_regexp", re.SOMEFLAG|re.SOMEOTHERFLAG)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,234
Messages
2,571,180
Members
47,813
Latest member
RustyGary3

Latest Threads

Top