re.RegexFlag
Five flags cover almost everything: I ignores case, M makes ^ and $ work per line, S lets . match newlines, X allows whitespace and comments, A restricts \w \d \s to ASCII. Each has a one-letter alias and an inline form.
Demo
import re (re.findall('python', 'Python PYTHON python'), re.findall('python', 'Python PYTHON python', re.IGNORECASE))
IGNORECASE uses Unicode case rules for str patterns, so straße matches STRAßE and ω matches Ω. MULTILINE only changes ^ and $; DOTALL only changes the dot — they are independent and are often used together. In VERBOSE mode a literal space must be escaped (\ ) or written as [ ] — and 'New\ York | Paris' shows alternation still works with the layout spaces removed.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| re.I / re.IGNORECASE | RegexFlag = 2 | no | Case-insensitive matching (Unicode case folding for str patterns). Inline: (?i). |
| re.M / re.MULTILINE | RegexFlag = 8 | no | ^ and $ also match at the start/end of every line. Inline: (?m). |
| re.S / re.DOTALL | RegexFlag = 16 | no | . also matches a newline. Inline: (?s). |
| re.X / re.VERBOSE | RegexFlag = 64 | no | Whitespace in the pattern is ignored and # starts a comment (except inside [...] or when escaped). Inline: (?x). |
| re.A / re.ASCII | RegexFlag = 256 | no | \w \W \b \B \d \D \s \S and case-insensitivity use ASCII only. Inline: (?a). |
| re.U / re.UNICODE | RegexFlag = 32 | no | The default for str patterns; kept for compatibility. Incompatible with re.ASCII. |
| re.L / re.LOCALE | RegexFlag = 4 | no | Locale-dependent matching — bytes patterns only; a ValueError with str. |
| re.NOFLAG | RegexFlag = 0 | no | No flags (3.11+) — a readable default for code that adds flags conditionally. |
Return value
re.RegexFlag — An enum.IntFlag; combine members with | and pass them as flags= (or put (?imsxa) at the start of the pattern).
Common patterns
import re pat = re.compile(r'^error:.*$', re.IGNORECASE | re.MULTILINE)
import re DATE = re.compile(r''' (?P<year>\d{4}) - # year (?P<month>\d{2}) - # month (?P<day>\d{2}) # day ''', re.VERBOSE)
import re re.compile(r'(?i)^subject:') re.compile(r'ID-(?i:[a-f0-9]+)')
import re flags = re.NOFLAG if ignore_case: flags |= re.IGNORECASE pat = re.compile(needle, flags)
Examples
Pitfalls
import re re.sub('a', '-', 'AaAa', re.I)
import re re.sub('a', '-', 'AaAa', flags=re.I)
import re re.compile(r'a(?i)b')
import re re.findall(r'a(?i:b)', 'ab aB Ab')
import re print(re.search(r'start.*end', 'start\nend', re.M))
import re re.search(r'start.*end', 'start\nend', re.S)
When to use
- IGNORECASE for user-facing search; MULTILINE for line-oriented text
- DOTALL when a match may span lines; VERBOSE for any pattern longer than one line
- ASCII when \w and \d must mean [a-zA-Z0-9_] and [0-9] (identifiers, protocols)
- LOCALE with str patterns — not allowed
- UNICODE — it is already the default for str
- IGNORECASE as a substitute for proper normalization (casefold, unicodedata)
Notes
FAQ
Pass re.IGNORECASE (or re.I): re.search(r'python', text, re.I). Or start the pattern with (?i). For part of a pattern use a scoped group: (?i:python).