9.7. Syntax Flag

9.7.1. Rationale

  • re.ASCII


  • re.LOCALE


  • re.DOTALL

  • re.UNICODE

  • re.VERBOSE

9.7.2. ASCII

  • Short: a

  • Long: re.ASCII

Make \w, \W, \b, \B, \d, \D, \s and \S perform ASCII-only matching instead of full Unicode matching


  • Short: i

  • Long: re.IGNORECASE

Case-insensitive (has Unicode support i.e. Ą and ą)

9.7.4. LOCALE

  • Short: L

  • Long: re.LOCALE

make \w, \W, \b, \B and case-insensitive matching dependent on the current locale


  • Short: m

  • Long: re.MULTILINE

match can start in one line, and end in another: ^ - start of line, $ - end of line

9.7.6. DOTALL

  • Short: s

  • Long: re.DOTALL

. matches also newlines (default newlines are not matched by .)

9.7.7. UNICODE

  • Short: u

  • Long: re.UNICODE

turns on UNICODE mode

9.7.8. VERBOSE

  • Short: x

  • Long: re.VERBOSE

ignores spaces (except \s) and allows for comments in in re.compile()

>>> import re
>>> a = re.compile(r"""\d +  # the integral part
...                    \.    # the decimal point
...                    \d *  # some fractional digits""", re.VERBOSE)
>>> b = re.compile(r"\d+\.\d*")

The final piece of regex syntax that Python's regular expression engine offers is a means of setting the flags. Usually the flags are set by passing them as additional parameters when calling the re.compile() function, but sometimes it's more convenient to set them as part of the regex itself. The syntax is simply (?flags) where flags is one or more of the following:

If the flags are set this way, they should be put at the start of the regex; they match nothing, so their effect on the regex is only to set the flags.

The letters used for the flags are the same as the ones used by Perl's regex engine, which is why s is used for re.DOTALL and x is used for re.VERBOSE.

Source: 1

9.7.9. References


Summerfield, Mark. Programming in Python 3. Regular Expressions. Chapter: 12. Pages: 445-465. Year: 2008. Retrieved: 2021-04-11. Publisher: Addison-Wesley Professional. ISBN: 978-0-13-712929-4. URL: https://www.informit.com/articles/article.aspx?p=1278986