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 get a group from a regex in Python?
- How to match whitespace in Python regex?
- How to regex match excluding a word in Python?
- How to replace in a file using Python regex?
- How to regex match excluding a character in Python?
- How to extract found values with Python regex?
- How to count matches with Python regex?
- How to match a URL path using Python regex?
- Python regex example
- How to make a case insensitive match with Python regex?
See more codes...