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
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With