By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,531 Members | 1,440 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,531 IT Pros & Developers. It's quick & easy.

re question

P: n/a
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
Jul 10 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In article <e8**********@news2.rz.uni-karlsruhe.de>,
Schüle Daniel <uv**@rz.uni-karlsruhe.dewrote:
>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 (je*@jes-2.demon.nl)

Jul 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.