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 replace all using Python regex?
- How to match zero or one occurence in Python regex?
- How to get a group from a regex in Python?
- How to match HTML tags with regex in Python?
- How to get all matches from a regex in Python?
- How to validate an IP using Python regex?
- How to perform a zero length match with Python Regex?
- How to match a hex number with regex in Python?
- How to match a float with regex in Python?
See more codes...