Regex pattern to find pec+

236 Views Asked by At

I am trying to match the "word" pec+ in the phrase

I cannot find pec+

I have tried the pattern \bpec\+\b, but there are no matches.

2

There are 2 best solutions below

0
On BEST ANSWER

Because + is not a word character, you need to end the expression by matching a non word boundary, using \B:

\bpec\+\B

See live demo, showing match for

foo pec+ bar

but not for any of:

foopec+ bar
foo pec+bar
foopec+bar
0
On

A word boundary ( \b ) is defined as a spot between two characters that has a \w on one side of it and and a \W on the other side of it (in either order), counting the imaginary characters off the beginning and end of the string as matching a \W .

@ and + being non word chars, there is no \w char to match.