Batu Lab NotesPractical developer guides

Use re.escape for a literal search token

By Batu · English technical notes

Also published in our Blogger archive.

A search token is not automatically a literal regular expression. In this example, + and ? in a+b? have regex meanings, so compiling the token directly would describe a pattern instead of the four visible characters. re.escape(token) returns a pattern fragment in which characters with regex significance are escaped. The compiled expression then finds the two literal occurrences in the input and ignores the unrelated aab text.

The assertions check both the matched strings and their zero-based spans. Spans are useful in diagnostics because they identify the precise part of the original string without modifying it. The printed count and first position make the result deterministic and easy to inspect.

Use re.escape for data that must become a literal portion of a regex pattern, including a token inserted into a larger pattern. It does not validate the surrounding pattern or provide a replacement string for re.sub; Python’s documentation specifically notes that replacement handling is different. It also does not decide whether substring matching is the desired rule: add boundaries or use fullmatch when the application needs them. This example uses only standard-library APIs available before Python 3.6; its f-strings make the shown program require Python 3.6+.

Reference: Python re.escape documentation.

AI-assistance disclosure: This article was drafted with AI assistance and should be reviewed in its application context.

import re

text = "a+b? aab x a+b?"
token = "a+b?"
pattern = re.compile(re.escape(token))
matches = list(pattern.finditer(text))

assert [match.group() for match in matches] == [token, token]
assert [match.span() for match in matches] == [(0, 4), (11, 15)]

print(f"matches={len(matches)}")
print(f"first={matches[0].start()}")
matches=2
first=0