using re.finditer()

Robert Brewer fumanchu at amor.org
Wed Oct 27 14:50:45 EDT 2004


Erik Johnson wrote:
> I am still fairly new to Python and trying to learn to 
> put RE's to good use. I am a little confused about the
> finditer() method...
> ...it doesn't document any restriction about multiline matching,
> but it certainly seems to have one...
> finditer() doesn't accept a flag like re.DOTALL, 
> as re.match() and re.search() do. It seems a shame not
> to be able to put an otherwise smart design to use.

Embed the flag(s) you desire in the regex itself. For example, to
include DOTALL, change r'<td.*?>(.*?)</td>' to r'(?s)<td.*?>(.*?)</td>'


>From the docs:

(?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, re.L, re.M, re.S, re.U, re.X) for the entire regular expression.
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.

    Note that the (?x) flag changes how the expression is parsed. It
should be used first in the expression string, or after one or more
whitespace characters. If there are non-whitespace characters before the
flag, the results are undefined.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list