Python (re)

Word Boundary (Whole Word Match) in PY

Match the whole word `cat` without matching it inside other words like `concatenate` or `category`.

Try it in the PY tester →

Pattern

regexPY
\bcat\b   (flags: gi)

Python (re) code

pyPython
import re

pattern = re.compile(r"\bcat\b", re.IGNORECASE)
input_text = "The cat sat on the mat."
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

\b is a zero-width assertion at a word boundary — the position between a word character (\w) and a non-word character (or string edge). Wrapping `cat` in two \b anchors forces the match to start AND end at word boundaries, so `cat` matches but `concat` and `cats` do not.

Examples

Input

The cat sat on the mat.

Matches

  • cat

Input

I will concatenate the strings.

No match

Input

CAT, Cat, cat — all match.

Matches

  • CAT
  • Cat
  • cat

Same pattern, other engines

← Back to Word Boundary (Whole Word Match) overview (all engines)