相关疑难解决方法(0)

正则表达式'(?<=#)[^#] +(?=#)'如何工作?

我在C#程序中有以下正则表达式,并且难以理解它:

(?<=#)[^#]+(?=#)
Run Code Online (Sandbox Code Playgroud)

我会把它分解为我认为我理解的内容:

(?<=#)    a group, matching a hash. what's `?<=`?
[^#]+     one or more non-hashes (used to achieve non-greediness)
(?=#)     another group, matching a hash. what's the `?=`?
Run Code Online (Sandbox Code Playgroud)

所以这个问题我已经是?<=?<组成部分.从阅读MSDN,?<name>用于命名组,但在这种情况下,尖括号永远不会关闭.

?=在文档中找不到,搜索它真的很难,因为搜索引擎大多会忽略那些特殊的字符.

regex lookahead lookbehind lookaround

20
推荐指数
1
解决办法
3521
查看次数

使用正则表达式以任意顺序匹配多个单词

好吧,假设我想匹配一个句子中的 3 个单词...但我需要以任何顺序匹配它们,例如:

$sentences = Array(
   "one two three four five six seven eight nine ten",
   "ten nine eight seven six five four three two one",
   "one three five seven nine ten",
   "two four six eight ten",
   "two ten four six one",
);
Run Code Online (Sandbox Code Playgroud)

所以我需要匹配单词“二”,“四”和“十”,但按任何顺序,它们之间可以有也可以没有任何其他单词。我尝试

foreach($sentences AS $sentence) {
   $n++;
   if(preg_match("/(two)(.*)(four)(.*)(ten)/",$sentence)) {
       echo $n." matched\n";
   }
}
Run Code Online (Sandbox Code Playgroud)

但这只会匹配句子 1,我需要匹配句子 1、2、4 和 5。

我希望你能帮忙...问候!(抱歉我的英语)

php regex

6
推荐指数
1
解决办法
4796
查看次数

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

我必须编写一些模式,这些模式将在用户输入时通过Regex用于匹配目的:

string pattern = "^.*overview\ of\ sales.*customer\ information.*$";
string input = "overview of sales with customer information";
Run Code Online (Sandbox Code Playgroud)

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

string pattern = "^.*overview\ of\ sales.*customer\ information.*$";
Run Code Online (Sandbox Code Playgroud)

也会匹配:

string input = "customer information with overview of sales";
Run Code Online (Sandbox Code Playgroud)

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

regex cpu-word

5
推荐指数
1
解决办法
4298
查看次数

标签 统计

regex ×3

cpu-word ×1

lookahead ×1

lookaround ×1

lookbehind ×1

php ×1