Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用于匹配三角形的正则表达式_Php_Regex_Preg Match All_Regex Negation_Regex Group - Fatal编程技术网

Php 用于匹配三角形的正则表达式

Php 用于匹配三角形的正则表达式,php,regex,preg-match-all,regex-negation,regex-group,Php,Regex,Preg Match All,Regex Negation,Regex Group,我试图从一个字符串中获取所有三个单词的组,这个字符串可以由多个句子组成,而不需要跨越句子边界。我让它适用于只有标准字母的单词: preg_match_all("/(?=(\b(\w+)(?:\s+(\w+)\b|$)(?:\s+(\w+)\b|$)))/",$utext,$matches); print_r($matches[1]); 但是,如果有撇号或连字符,它就会下降。因此,通过这个示例文本: The quick brown fox's feet jumped over the lazy

我试图从一个字符串中获取所有三个单词的组,这个字符串可以由多个句子组成,而不需要跨越句子边界。我让它适用于只有标准字母的单词:

preg_match_all("/(?=(\b(\w+)(?:\s+(\w+)\b|$)(?:\s+(\w+)\b|$)))/",$utext,$matches);
print_r($matches[1]);
但是,如果有撇号或连字符,它就会下降。因此,通过这个示例文本:

The quick brown fox's feet jumped over the lazy dog. The rain falls head-first in the plain.
我想要这份清单:

快速棕色 快褐狐 棕狐爪 狐狸的脚跳了起来 脚跳了过去 跳过 懒汉 懒狗 下雨了 雨先下 头一个倒下 在比赛中领先 在平原上 我已经尝试对上面的每个\w使用[\w'-],但这会带来一些奇怪之处:

Array ( [0] => The quick brown [1] => quick brown fox's [2] => brown fox's feet [3] => fox's feet jumped [4] => 's feet jumped [5] => s feet jumped [6] => feet jumped over [7] => jumped over the [8] => over the lazy [9] => the lazy dog [10] => The rain falls [11] => rain falls head-first [12] => falls head-first in [13] => head-first in the [14] => -first in the [15] => first in the [16] => in the plain )
我错过了什么?谢谢。

只需将\w改为[^\s.]而不是空格或点,然后删除boudaries这个词。另一个更改是在正则表达式的开头添加行或空格的交替开头:

$text = "The quick brown fox's feet jumped over the lazy dog. The rain falls head-first in the plain.";

preg_match_all("/(?=((?<=^|\s)[^\s.]+(?:\s+[^\s.]+|$)(?:\s+[^\s.]+|$)))/",$text,$matches);
print_r($matches[1]);
正则表达式解释:

?=向前看 第一组开始 ? 快速棕色 [1] =>快速棕色狐狸 [2] =>棕色狐狸的脚 [3] =>狐狸的脚跳了起来 [4] =>她的脚跳了起来 [5] =>脚跳了过去 [6] =>跳过 [7] =>超过懒惰的人 [8] =>懒狗 [9] =>下雨了 [10] =>雨是头先下的 [11] =>排名第一 [12] =>头先入 [13] =>在平原上 [14] =>这是一个 [15] =>是一个报价 [16] =>就是这样 正则表达式解释:

?=向前看 第一组开始
太好了,谢谢。我现在看到它很难用引号和逗号,比如我说的“这是一个引号”,就是这样。但我可能要求太多了!在第二个例子中,合法的三角形是:这是一个,是一个引号,这是因为我说的是一个太短而不能限定的子句。我想这可能比正则表达式更深入到NLP中。@thehattery:您可以在字符类中添加逗号,但是,对于单引号,它有点复杂,因为您希望在fox中保留引号。尝试将[^\s.]+替换为?:?抱歉,:?@thehattery:Try:preg\u match\u all/?=?