Dawid PR
Dawid PR

Reputation: 127

Regex match specified number of words in string

Is there an option in regex to tell how many from passed words in match must find ? I have regex:

/\b(brasserie|desbrosses)\b/

and string:

"brasserie desbrosses"

Now this regex find me two match, but i want to tell him how many words should it match to return positive result. So if i tell him return me positive match for 2 words and i will have string: "brasserie villa" it shouldn't return me positive match, because only one word match regex. So is there an option to do this ? and of course for other number of words, like 3, 4...

Upvotes: 0

Views: 99

Answers (1)

anubhava
anubhava

Reputation: 784998

You can use:

(\b(?:brasserie|desbrosses)\b\s*){2}

to force match to succeed with at least 2 matches of given match words.

RegEx Demo

Upvotes: 1

Related Questions