Compilation flags

When compiling a pattern string into a pattern object, it's possible to modify the standard behavior of the patterns. In order to do that, we have to use the compilation flags. These can be combined using the bitwise OR "|".

Flag

Python

Description

re.IGNORECASE or re.I

2.x

3.x

The pattern will match lower case and upper case.

re.MULTILINE or re.M

2.x

3.x

This flag changes the behavior of two metacharacters:

  • ^: Which now matches at the beginning of the string and at the beginning of each new line.
  • $: In this case, it matches at the end of the string and the end of each line. Concretely, it matches right before the newline character.

re.DOTALL or re.S

2.x

3.x

The metacharacter "." will match any character even the newline. ...

Get Mastering Python Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.