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?
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?
Share Improve this question asked Oct 29, 2015 at 1:53 MaxMax 5194 silver badges7 bronze badges 1-
Maybe
(?:[^']|^)(hi|ll|this|that|etc)\b
: regex101./r/gD7aC0/1 – user4227915 Commented Oct 29, 2015 at 2:19
2 Answers
Reset to default 4If 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.
Use the apostrophe in addition to \b
to begin and end a match:
(?:\b|')(hi|ll|this|that|etc)(?:\b|')
(?:...)
means a non-capturing group. Stub on Regex101