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

Tim Chase python.list at tim.thechases.com
Sat May 23 16:20:31 EDT 2009


> 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







More information about the Python-list mailing list