问题
I'm looking for a way to find two or more words in a sentence that might come in any order. For example I might look for the words "NCAA" and "Basketball" in a sentence that says NCAA Indiana Vs. Purdue Basketball" or it might say "Indiana Vs. Purdue Basketball NCAA". How would I write the regex for the words showing in any order and any location in the string?
回答1:
You can use lookahead:
(?=.*?NCAA)(?=.*?Basketball)
Or else use alternation:
NCAA.*?Basketball|Basketball.*?NCAA
来源:https://stackoverflow.com/questions/26041258/java-regex-looking-a-combination-of-words-in-any-order