Regex: capture group of max number of sequential defined words -


regex: cat|dog|mouse|fish

on

text: dog cat

captures group 2 matches. group single match of "dog cat".

i tried [cat|dog|mouse|fish]+ still 2 matches, , matches other things?

add optional \s or \s* before pattern , make repeat 1 or more times.

\b(?:\s?(?:cat|dog|mouse|fish))+ 

[cat|dog|mouse|fish] wrong way of matching group of characters. need put substrings inside group not inside character class.

demo

or

(?<nnn>\b(?:cat|dog|mouse|fish)(?:\s+(?:cat|dog|mouse|fish)\b)*) 

demo


Comments