Java Regex looking a combination of words in any order

≡放荡痞女 提交于 2019-12-24 11:50:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!