Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how add flags to python regex?

I want to add a flag to my re.sub regex string. In PHP, I would just do this '\test is good\i'

I tried this in re.compile, but it does not have .sub method. I tried to use s.replace, but I cannot add the i flag to this as well

like image 814
Peter Hayman Avatar asked Sep 19 '25 01:09

Peter Hayman


1 Answers

Compiled regex objects can be passed to re.sub(), so flags can still be passed in at compile time.

r = re.compile('test is good', re.IGNORECASE)
re.sub(r, 'yup', 'TEST IS GOOD')

Alternately, flags can be added with (?iLmsux) syntax:

re.sub('(?i)test is good', 'yup', 'TEST IS GOOD')
like image 151
Charles Duffy Avatar answered Sep 21 '25 15:09

Charles Duffy