I would like to pass certain flags into a re.compile function based on logic similar to the below. I was wondering whether it's possible to do so.
flags = ""
if multiline:
flags = 're.M'
if dotall:
flags = flags + '|re.S'
if verbose:
flags = flags + '|re.X'
if ignorecase:
flags = flags + '|re.I'
if uni_code:
flags = flags + '|re.U'
regex = re.compile(r'Test Pattern', flags)
re
flags are just numbers. So, we need to binary OR them, like this
flags = 0
if multiline:
flags = re.M
if dotall:
flags |= re.S
if verbose:
flags |= re.X
if ignorecase:
flags |= re.I
if uni_code:
flags |= re.U
regex = re.compile(r'Test Pattern', flags)
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