ignore case only for a part of the regex?

Vlastimil Brom vlastimil.brom at gmail.com
Sun Dec 30 11:38:31 EST 2012


2012/12/30 Helmut Jarausch <jarausch at skynet.be>:
> Hi,
>
> is there a means to specify that 'ignore-case' should only apply to a part
> of a regex?
>
> E.g.
>
> the regex should match  Msg-id:, Msg-Id, ...  but not  msg-id: and so on.
>
> I've tried the pattern
> r'^Msg-(?:(?i)id):'
> but (?i) makes the whole pattern ignoring case.
>
> In my simple case I could say
> r'Msg-[Ii][Dd]:'
> but that's a bit clumsy.
>
> Is there a more elegant way? Is there a way to compose a pattern
> from subpatterns which are compiled with different flags?
>
> Many thanks for a hint,
> Helmut.
> --
> http://mail.python.org/mailman/listinfo/python-list

Hi,
you may check the new regex implementation for python
http://pypi.python.org/pypi/regex
which allows (among many other improvements) for scoped flags:

>>> import regex
>>> regex.findall(r"Msg-(?i:id):", "the regex should match  Msg-id:, Msg-Id:, ...  but not  msg-id:, MSG-ID:  and so on")
['Msg-id:', 'Msg-Id:']
>>>

hth,
 vbr



More information about the Python-list mailing list