简体   繁体   中英

Finding two words anywhere in text with regular expression

I'm not an expert at regular expressions by any stretch of the imagination! I understand the basics of how regex comes together, but through a regular expression, can I search for two words that could appear anywhere within a piece of text?
ie the words hot and weather. Could be written as: the weather was hot during the hot weather the weather has become even hotter

Is it possible that a regex can be created to pick up all three scenarios, but not (for example) the picture was shot in poor weather?

Any help would be appreciated - I'm working in PHP5.6 by the way alternative is there a better way to do it that I haven't thought of?

If you just need those two words you could have the regex search with an optional list of word endings.

For Example: (\\bweather\\b.*hot(ter|test)?\\b|\\bhot(ter|test)?\\b.*\\bweather\\b)

But if you need to build the regex from a user's input you would want to have a full list of possible endings: (s|er|est|ier|iest|ter|test|etc|etc)?

Example: (\\bweather(s|er|est|ier|iest|ter|test|etc|etc)?\\b.*hot(s|er|est|ier|iest|ter|test|etc|etc)?\\b|\\b(s|er|est|ier|iest|ter|test|etc|etc)?\\b.*\\bweather(s|er|est|ier|iest|ter|test|etc|etc)?\\b)

The only problem is it would miss endings like silly being silliest or see being saw without adding additional logic to look at the original words.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM