re question

  • Thread starter =?ISO-8859-1?Q?Sch=FCle_Daniel?=
  • Start date
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

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?

Regards, Daniel
 
J

Jim Segrave

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.
 

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

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top