加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

用正则表达式查找任何顺序的单词

发布时间:2020-12-14 02:30:27 所属栏目:百科 来源:网络整理
导读:考虑到这些条目…… red yellow greenyellow green redgreen red yellow other red yellow other green red 我可以按任意顺序(前3行)匹配红色,黄色,绿色: ^(^red|^yellow|^green) (red|yellow|green) (red$|yellow$|green$)$ 请注意,我需要找到这些词,没有
考虑到这些条目……
red yellow green
yellow green red
green red yellow 
other red yellow 
other green red

我可以按任意顺序(前3行)匹配红色,黄色,绿色:

^(^red|^yellow|^green) (red|yellow|green) (red$|yellow$|green$)$

请注意,我需要找到这些词,没有别的.
但是当我有更多的话时,表达式会增长很多.

例如(有4个字)

^(^red|^yellow|^green|^black) (red|yellow|green|black) (red|yellow|green|black) (red$|yellow$|green$|black$)$

我的问题是:还有其他更简单的方法来使用正则表达式吗?

如果你可以容忍红色的红色黄色(好吧,我认为你的正则表达式可以匹配这样的线条)那么你需要的正则表达式是
^(red|yellow|green)( (red|yellow|green))*$

你可以在那里测试它:https://regex101.com/r/fC3pM3/1(它也有很好的解释)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读