re question

Jim Segrave jes at nl.demon.net
Mon Jul 10 16:25:58 EDT 2006


In article <e8tn4k$1vo$1 at news2.rz.uni-karlsruhe.de>,
Schüle Daniel  <uval at rz.uni-karlsruhe.de> wrote:
>Hello,
>
>consider the following code
>
> >>> re.search("[a-z](?i)[a-z]","AA")
><_sre.SRE_Match object at 0x40177e20>
>
>this gives a match
>if we provide an extra group for the first character it still works
>
> >>> re.search("([a-z])(?i)[a-z]","AA").group(1)
>'A'
> >>>
>
>it doesn't matter where (?i) is placed, right?
>the re engine would glance at once on the entire pattern string
>analize it (complain if pattern doesn't make sense, eg invalid)
>and it would be the same as if the option was given expicitely
>as re.IGNORECASE.
>
>Is there a way to switch-off the option resp.
>switch-on the option in the middle of the pattern?

The docs say:

(?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.


Some regex packages, but not Python's, support (?-<flag>) and this
allows turning the flag off and on for parts of the regex.



-- 
Jim Segrave           (jes at jes-2.demon.nl)




More information about the Python-list mailing list