Learn regular expressions by seeing patterns, test cases, and match results side-by-side
Control where matches occur using anchors and word boundaries
The caret ^ anchor matches the start of a line (with MULTILINE flag) or string.
Pattern breakdown:
^Hello - "Hello" must be at the start of a lineThis matches only lines that begin with "Hello", not "Hello" appearing elsewhere.
Hello World Say Hello Hello there hello
The dollar sign $ anchor matches the end of a line or string.
Pattern breakdown:
\. - literal period (escaped because . is a special character)$ - must be at end of lineThis matches only lines ending with a period.
Source: Python Regex HOWTO
This is a sentence. Another sentence End with period. No period here
The \b word boundary matches the position between a word character and a non-word character.
Pattern breakdown:
\bcat\b - "cat" as a complete wordThis is essential for finding whole words and avoiding partial matches.
I have a cat. The catalog is here. cat scattered cats