How to pass multiline flag to re.sub without using re.complie.

S

samba

I have a regex that needs multiline flag. Some where I read I can pass
multiline flag in regex string itself without using re.compile. If
anybody have any idea about how to do that please reply.
 
T

Tim Chase

I have a regex that needs multiline flag. Some where I read I
can pass multiline flag in regex string itself without using
re.compile. If anybody have any idea about how to do that
please reply.


As detailed at [1],

"""
(?iLmsux)

(One or more letters from the set 'i', 'L', 'm', 's', 'u', 'x'.)
The group matches the empty string; the letters set the
corresponding flags: re.I (ignore case), re.L (locale dependent),
re.M (multi-line), re.S (dot matches all), re.U (Unicode
dependent), and re.X (verbose), for the entire regular
expression. (The flags are described in Module Contents.) This is
useful if you wish to include the flags as part of the regular
expression, instead of passing a flag argument to the compile()
function.
"""

So you should be able to insert "(?m)" at the beginning of your
regexp to make it multiline.

-tkc


[1]
http://docs.python.org/library/re.html
 
M

MRAB

samba said:
I have a regex that needs multiline flag. Some where I read I can pass
multiline flag in regex string itself without using re.compile. If
anybody have any idea about how to do that please reply.

Include "(?m)" in the regular expression for multiline matching. It's
best to put it at the start.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top