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

在任何单词顺序正则表达式匹配字符串

发布时间:2020-12-14 02:30:06 所属栏目:百科 来源:网络整理
导读:我必须编写一些模式,这些模式将在用户输入时通过 Regex用于匹配目的: string pattern = "^.*overview of sales.*customer information.*$";string input = "overview of sales with customer information"; 有没有办法删除正则表达式中的单词顺序?所以
我必须编写一些模式,这些模式将在用户输入时通过 Regex用于匹配目的:
string pattern = "^.*overview of sales.*customer information.*$";
string input = "overview of sales with customer information";

有没有办法删除正则表达式中的单词顺序?所以

string pattern = "^.*overview of sales.*customer information.*$";

也会匹配:

string input = "customer information with overview of sales";

这可以通过以相反的顺序编写每个模式来完成,但是因为模式的数量很少并且将随着时间和数量*而增长.这往往是繁琐的做法,所以请指导这件事.

你想要使用 positive lookahead?=.*.

[Positive lookahead]q(?=u)matches a q that is followed by a u,without making the u part of the match. The positive lookahead construct is a pair of parentheses,with the opening parenthesis followed by a question mark and an equals sign.

在这种情况下可以使用正向前瞻来匹配任何顺序的一组模式,如下所示:

(α=.*)(?=.*客户)(?=.*资讯)(?=.*用)(?=.*概述)(?=.*销售)

Debuggex Demo Here

要修改,只需根据需要添加,编辑或删除单词.

编辑

在这里找到一个解释相同概念的问题:https://stackoverflow.com/a/3533526/2081889

(编辑:李大同)

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

    推荐文章
      热点阅读