10

Can anyone tell me if I can combine flags like re.IGNORECASE, re.MULTILINE and re.DOTALL for regular expression matching?

r = re.compile(regex, re.IGNORECASE | re.MULTILINE | re.DOTALL)

I need to match an entire paragraph or an expression in one line according to the use case.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Elhabib
  • 141
  • 2
  • 10
  • https://docs.python.org/3/library/re.html#re.compile — "The expression’s behaviour can be modified by specifying a flags value. Values can be any of the following variables, combined using bitwise OR (the `|` operator)" – khelwood Feb 10 '21 at 15:40
  • I am using it, but still find an issue seems not interpreted – Elhabib Feb 10 '21 at 16:04
  • i used it like this: r = re.compile(regex, re.IGNORECASE | re.MULTILINE | re.DOTALL ) matches = list(r.finditer(log)) – Elhabib Feb 10 '21 at 16:09

1 Answers1

7

Yes, you can combine regex flags with |.

The documentation https://docs.python.org/3/library/re.html#re.compile specifically says:

The expression’s behaviour can be modified by specifying a flags value. Values can be any of the following variables, combined using bitwise OR (the | operator).

khelwood
  • 55,782
  • 14
  • 81
  • 108