3.5 Compilation Flags Compilation flags let you modify some aspects of how regular expressions work. Flags are available in the$_{re}$ module under two names, a long name such as$_{IGNORECASE}$ and a short, one-letter form such as$_{I}$. (If you’re familiar with Perl’s pattern modifiers, the one-letter forms use the same letters; the short form of re.VERBOSE is re.X , for example.) Multiple flags can be specified by bitwise OR-ing them; re.I | re.M sets both the I and M flags, for example. Here’s a table of the available flags, followed by a more detailed explanation of each one. 8 Flag Meaning ASCII , A Makes several escapes like \w , \b , \s and \d match only on ASCII characters with the respective property. DOTALL , S Make . match any character, including newlines. IGNORECASE , I Do case-insensitive matches. LOCALE , L Do a locale-aware match. MULTILINE , M Multi-line matching, affecting ^ and $ . VERBOSE , X (for ‘ex- tended’) Enable verbose REs, which can be organized more cleanly and understandably. I IGNORECASE L LOCALE M MULTILINE S DOTALL Perform case-insensitive matching; character class and literal strings will match letters by ignoring case. For example,$_{[A-Z]}$ will match lowercase letters, too. Full Unicode matching also works unless the$_{ASCII}$ flag is used to disable non-ASCII matches. When the Unicode patterns$_{[a-z]}$or$_{[A-Z]}$ are used in combina- tion with the IGNORECASE flag, they will match the 52 ASCII letters and 4 additional non-ASCII letters: ‘İ’ (U+0130, Latin capital letter I with dot above), ‘ı’ (U+0131, Latin small letter dotless i), ‘ſ’ (U+017F, Latin small letter long s) and ‘K’ (U+212A, Kelvin sign). $_{Spam}$will match$_{'Spam'}$ ,$_{'spam'}$ ,$_{'spAM'}$ , or 'ſpam' (the latter is matched only in Unicode mode). This lowercasing doesn’t take the current locale into account; it will if you also set the$_{LOCALE}$ flag. Make$_{\w}$,$_{\W}$ ,$_{\b}$,$_{\B}$ and case-insensitive matching dependent on the current locale instead of the Unicode database. Locales are a feature of the C library intended to help in writing programs that take account of language differences. For example, if you’re processing encoded French text, you’d want to be able to write $_{\w+}$to match words, but $_{\w}$only matches the character class $_{[A-Za-z]}$in bytes patterns; it won’t match bytes corresponding to é or $_{ç}$. If your system is configured properly and a French locale is selected, certain C functions will tell the program that the byte corresponding to é should also be considered a letter. Setting the LOCALE flag when compiling a regular expression will cause the resulting compiled object to use these C functions for$_{\w}$; this is slower, but also enables$_{\w+}$to match French words as you’d expect. The use of this flag is discouraged in Python 3 as the locale mechanism is very unreliable, it only handles one “culture” at a time, and it only works with 8-bit locales. Unicode matching is already enabled by default in Python 3 for Unicode (str) patterns, and it is able to handle different locales/languages. ($_{^}$ and$_{$}$haven’t been explained yet; they’ll be introduced in section More Metacharacters .) Usually$_{^}$ matches only at the beginning of the string, and$_{$}$matches only at the end of the string and immedi- ately before the newline (if any) at the end of the string. When this flag is specified,$_{^}$ matches at the beginning of the string and at the beginning of each line within the string, immediately following each newline. Similarly, the$_{$}$metacharacter matches either at the end of the string and at the end of each line (immediately preceding each newline). Makes the$_{'.'}$ special character match any character at all, including a newline; without this flag, '.' will match anything except a newline.