Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match list of words without the list of chars around

Tags:

I have this regex

(?:$|^| )(one|common|word|or|another)(?:$|^| ) 

which matches fine unless the two words are next to each other.

One one's more word'word common word or another word more another   More and more years to match one or more other strings  And common word things and or 

In the above it matches one in line two but not the or just next to it. Same for common and word int the third line.

Live Example: http://regex101.com/r/hV3wQ3

I believe it's something to do with the non-matching groups' number. But, I am not sure how to achieve the end goal of matching all the list of words without any char around them.

I do not want the one in one's or the word in word'word to be matched.

like image 991
San Avatar asked Jan 30 '14 04:01

San


People also ask

How do you search for multiple words in a regular expression?

However, to recognize multiple words in any order using regex, I'd suggest the use of quantifier in regex: (\b(james|jack)\b. *){2,} . Unlike lookaround or mode modifier, this works in most regex flavours.

What is a word boundary regex?

A word boundary, in most regex dialects, is a position between \w and \W (non-word char), or at the beginning or end of a string if it begins or ends (respectively) with a word character ( [0-9A-Za-z_] ). So, in the string "-12" , it would match before the 1 or after the 2.

What is a capturing group regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .


2 Answers

Since your capture groups define explicitly one character on either side of the common word, it's looking for space word space and then when it doesn't find another space, it fails.

In this case, since you don't want to match all the characters word boundary's would catch (period, apostrophe, etc.) you need to use a bit of trickery with lookaheads, lookbehinds, and non-capture groups. Try this:

(?:^|(?<= ))(one|common|word|or|another)(?:(?= )|$) 

http://regex101.com/r/cM9hD8

Word boundaries are still simpler to implement, so for reference sake, you could also do this (though it would include ', ., etc.).

\b(one|common|word|or|another)\b 
like image 150
brandonscript Avatar answered Sep 20 '22 09:09

brandonscript


You can use (?:[\s]|^)(one|common|word|or|another)(?=[\s]|$) instead.

It will not match one's , someone ,etc...

Check DEMO

like image 45
Sujith PS Avatar answered Sep 19 '22 09:09

Sujith PS