Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unset a flag from within a python regex

Tags:

python

regex

I have a large collection of regular expressions, and I apply them all using a few flags with something like:

re.search(aRegex,aString,flags=re.IGNORECASE | re.UNICODE)

unfortunately, there are one or two for which I don't want to ignore case. Python provides a handy way to set flags from within a regular expression (eg: r'(?iu)...' sets the re.IGNORECASE and re.UNICODE flags).

Is there a way that I can unset flags from within an expression? Perhaps somerthing like:

r'(?i-)...'

or

r'(?I)...'

to force case sensitivity. (btw: neither of those work..)

like image 256
drevicko Avatar asked Sep 01 '25 10:09

drevicko


1 Answers

There is no way to unset a flag once you set it (in the regex or in a function) in the Python 2.x re module. (There is also no way to set a flag for a portion of the regex either).

Consider using regex package if you need such feature.

like image 167
nhahtdh Avatar answered Sep 03 '25 00:09

nhahtdh