Delving Deeper

Regular Expressions

Embedding Flags and Patterns

In situations where flags cannot be added when compiling an expression, such as when a pattern is passed as an argument to a library function that will compile it later, the flags can be embedded inside the expression string itself. For example, to turn case-insensitive matching on, add (?i) to the beginning of the expression.


# re_flags_embedded.py

import re

text = 'This is some text -- with punctuation.'
pattern = r'(?i)\bT\w+'
regex = re.compile(pattern)

print('Text      :', text)
print('Pattern   :', pattern)
print('Matches   :', regex.findall(text))

Because the options control the way the entire expression is evaluated or parsed, they should always appear at the beginning of the expression.


$ python3 re_flags_embedded.py

Text      : This is some text -- with punctuation.
Pattern   : (?i)\bT\w+
Matches   : ['This', 'text']

The abbreviations for all of the flags are listed in the table below.

Regular Expression Flag Abbreviations
Flag Abbreviation
ASCII a
IGNORECASE i
MULTILINE m
DOTALL s
VERBOSE x

Embedded flags can be combined by placing them within the same group. For example, (?im) turns on case-insensitive matching for multiline strings.