Allow changing regular expression options not at the start of the string

Today Pythong does not allow changing regex options which happen to be not at the start of the string. For instance:

import re
s = "I luv Python and CPython"
matches = re.finditer(r"\b(?i)[A-Z](?-i)[a-z]+\b", s)

As you can see, the first character in the bounded string can be of any case (?i), but starting from the second character, all of them must be in lower-case since (?-i) cancels insensitiveness of the first option. In .NET it gives expected result:

'luv'
'Python'
'and'

CPython was excluded because second character P is upper-case, when it must be lower-case.
Is it possible to realize same in Python?

You can, you just have to do it like the documentation describes.

This

import re

s = "I luv Python and CPython"
matches = re.finditer(r"(?i)\b[A-Z](?-i:[a-z]+)\b", s)
print([m.group(0) for m in matches])

Gives you this

['luv', 'Python', 'and']

You can use scoped modifiers:

r"\b(?i:[A-Z])[a-z]+\b"
2 Likes