Regex Flags

Carson West

Regular Expressions

Regex Flags

Python’s re module offers several flags that modify the behavior of regular expression operations. These flags are used as optional arguments to functions like re.compile(), re.search(), re.findall(), etc. They are combined using the bitwise OR operator (|).

Commonly Used Flags:

import re

text = "Hello World"
match = re.search("world", text, re.IGNORECASE)
print(match) # <re.Match object; span=(6, 11), match='World'>
import re

text = """line one
line two
line three"""
matches = re.findall(r"^line", text, re.MULTILINE)
print(matches) # ['line', 'line', 'line']]
import re

text = "line one\nline two"
match = re.search(r".*", text, re.DOTALL)
print(match) # <re.Match object; span=(0, 16), match='line one\nline two'>
match = re.search(r".*", text) #Without DOTALL
print(match) # <re.Match object; span=(0, 9), match='line one'>
import re

text = "Hello 世界"
match = re.findall(r"\w+", text) #Without ASCII flag
print(match) # ['Hello', '世界']]
match = re.findall(r"\w+", text, re.ASCII) #With ASCII flag
print(match) # ['Hello']]
import re

pattern = re.compile(r"""
    \d+         # one or more digits
    \s+         # one or more whitespace characters
    [a-zA-Z]]+   # one or more letters
""", re.VERBOSE)

text = "123 abc"
match = pattern.search(text)
print(match) #<re.Match object; span=(0, 8), match='123 abc'>

Regex Special Characters
Regex Compilation