Java Regex以任意顺序查看单词的组合 [英] Java Regex looking a combination of words in any order

查看:289
本文介绍了Java Regex以任意顺序查看单词的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来在一个句子中找到两个或更多的单词,这个单词可能会以任何顺序出现。例如,我可能会在NCAA印第安纳VS的句子中查找NCAA和Basketball。普渡大学篮球或者可能会说印第安纳VS.普渡大学篮球NCAA。我怎样才能写出任何顺序和字符串中的任何位置显示的正则表达式?

你可以使用前瞻:

$ p $ (?=。*?NCAA)(?=。*?篮球)

或者使用替换:

  NCAA。*?Basketball | Basketball。*?NCAA 


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?

解决方案

You can use lookahead:

(?=.*?NCAA)(?=.*?Basketball)

Or else use alternation:

NCAA.*?Basketball|Basketball.*?NCAA

这篇关于Java Regex以任意顺序查看单词的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆