Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression match array of words excluding words found in contractions

Say I have an array of words, for example: (hi|ll|this|that|etc) and I want to find it in the following text:

Hi, I'll match this and ll too

I'm using: \\b(hi|ll|this|that|etc)\\b

But I want to only match whole words, excluding words found in contractions. Basically treat apostrophes as another "word seperator". In this case, it shouldn't match the "ll" in "I'll".

Ideas?

like image 786
Max Avatar asked Oct 17 '25 14:10

Max


1 Answers

If you want match just words you can try with:

(?:^|(?=[^']).\b)(hi|ll|th(?:is|at)|etc)\b

DEMO

and get words with group 1. However the \b will still allow to match fragments like: -this or @ll. I don't know is it desired result.

like image 129
m.cekiera Avatar answered Oct 20 '25 05:10

m.cekiera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!