I have the following scenario where I have a set of phrases, some are couple of words, some are words alone, I'd like to match all this phrases but with my current approach I end up matching single words
For Example:
Things I could match:
Regex: (sunny( )day|sunny( )valley|day)
Sentence: today is a sunny day
here, I hope that "sunny day" is matched however, my regex always matches "day", there might be other sentences that contain day alone that I want to match.
Does anyone know how to design a regex that accomplish this task?
Your regex matches "day" inside "today" as it is the leftmost (first) "day" substring in the input string.
Use word boundaries to match whole words only:
\b(sunny( )day|sunny( )valley|day)\b
See regex demo
To not generate additional matches of spaces. Following OP's comment. I suggest this Regex:
\\b(sunny\\sday|sunny\\svalley|day)\\b
(demo)
Ps.:
\\s
is for spaces
Parentheses are not useful, because matching of spaces is not desirable in this case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.