How does B regular expression work in Python?


The word boundary \b matches positions where one side is a word character (usually a letter, digit or underscore)

\B matches all positions where \b doesn't match.

The following code shows how regexpr \B works

import re
result = re.findall(r'\Bcat', 'certificate')
result2 = re.findall(r'\Bcat', 'tomcat')
result3 = re.findall(r'\Bcat', 'catfish')
print result, result2,result3

This gives the output

['cat'] ['cat'] []

Updated on: 16-Jun-2020

417 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements