re question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Schüle Daniel

    #1

    re question

    Hello,

    consider the following code
    >>re.search("[a-z](?i)[a-z]","AA")
    <_sre.SRE_Mat ch 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?

    Regards, Daniel
  • Jim Segrave

    #2
    Re: re question

    In article <e8tn4k$1vo$1@n ews2.rz.uni-karlsruhe.de>,
    Schüle Daniel <uval@rz.uni-karlsruhe.dewro te:
    >Hello,
    >
    >consider the following code
    >
    >re.search("[a-z](?i)[a-z]","AA")
    ><_sre.SRE_Matc h 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@jes-2.demon.nl)

    Comment

    Working...