What would be the correct syntax to use with the inline (?aiLmsux)
flags in the re module
? For example:
string = 'Hello There'
re.search(r'^(?i[a-z]+)(\s[a-z]+)?', string)
The above is an invalid python expression, but basically I would like the first [a-z]+
to do a case-insensitive match, so the matched string will be "Hello".
The closest I have been able to get is:
>>> re.search(r'^(?i)([a-z]+)(\s[a-z]+)?', string).group()
'Hello There'
# also, not what I want
>>> re.search(r'^([a-z]+)(\s[a-z]+)?', string, re.I).group()
'Hello There'
But this flag is working on the entire string, and not just the first [a-z]+
part. How could I limit the scope of the ?i
?
Update: Note, the linked duplicate shows how to use the re.I
flag as well as the (?i)
flag on an entire string, but I'm looking how (if?) it's possible to only apply that flag on a grouped sub-expresssion.
The equivalent regex should be:
# Only the first part -- [a-zA-Z] is made case-insensitive
>>> re.search(r'^[a-zA-Z]+(\s[a-z]+)?', string).group()
'Hello'