python-regexHow to use word boundaries in Python Regex?
Word boundaries are used in Python Regex to match the beginning or end of a word.
import re
# Match the beginning of a word
result = re.search(r'\bcat', 'The cat in the hat')
print(result.group())
Output example
cat
Code explanation
\b: A word boundary which matches the beginning or end of a wordcat: The word to matchThe cat in the hat: The string to search
Helpful links
More of Python Regex
- How to split using Python regex?
- How to match a question mark in Python regex?
- How to match a YYYY-MM-DD date with Python Regex?
- How to match whitespace in Python regex?
- How to match a UUID using Python regex?
- How to match a year with Python Regex?
- How to remove numbers from a string using Python regex?
- How to match a URL using Python regex?
- How to match a plus sign in Python regex?
See more codes...