繁体   English   中英

如何使用PHP正则表达式在字符串中搜索包含重复单词的单词序列?

[英]How to use PHP regular expressions to search a string for word sequences containing repeated words?

我使用PHP来计算字符串中单词序列的出现次数。 在以下示例中,我没有得到我希望看到的结果。

$subject1 = " [word1 [word1 [word1 [word1 [word3 ";
$pattern1 = preg_quote("[word1 [word1", '/');
echo "count of '[word1 [word1'=". preg_match_all("/(\s|^|\W)" . $pattern1 . "(?=\s|$|\W)/", $subject1, $dummy) . "<br/>"; 

$subject2 = " [word1 [word2 [word1 [word2 [word1 [helloagain ";
$pattern2 = preg_quote("[word1 [word2 [word1", '/');
echo "count of '[word1 [word2 [word1'=". preg_match_all("/(\s|^|\W)" . $pattern2 . "(?=\s|$|\W)/", $subject2, $dummy) . "<br/>";

以上回报:

count of '[word1 [word1'=2
count of '[word1 [word2 [word1'=1

我希望结果如下:

count of '[word1 [word1'=3 // there are 3  instances of ‘[word1 [word1’ in $subject1
count of '[word1 [word2 [word1'=2 // // there are 2  instances of [word1 [word2 [word1’ in $subject2

word in the matched substring. 实现此目的的一种方法是每次在主题中找到模式时,下一个搜索应该从匹配子字符串中的单词开始。 可以构建这样的正则表达式吗? 谢谢。

使用mb_substr_count

substr_count不计算重叠值,但我不知道为什么, mb_substr_count确实如此

$subject1 = " [word1 [word1 [word1 [word1 [word3 ";
echo mb_substr_count($subject1, "[word1 [word1"); // 3
echo mb_substr_count($subject1, "[word1 [word1 [word1"); // 2

编辑:

备查,

显然mb_substr_count在php 5.2上的行为与php 5.3不同。 我想这个函数的正确行为应该与substr_count相同,仅用于多字节支持,并且由于substr_count不支持重叠,所以substr_count也应该mb_substr_count

所以,虽然这个答案适用于php 5.2.6,但是不要使用它,否则当你更新php版本时可能会遇到问题。

而不是preg_match_all,我在preg_match上使用带有offset的while循环:

$subject1 = " [word1 [word1 [word1 [word1 [word3 ";
$pattern1 = preg_quote("[word1 [word1", '/');
$offset=0;
$total=0;
while($count = preg_match("/(?:\s|^|\W)$pattern1(?=\s|$|\W)/", $subject1, $matches, PREG_OFFSET_CAPTURE, $offset)) {
    // summ all matches
    $total  += $count;
    // valorisation of offset with the position of the match + 1
    // the next preg_match will start at this position
    $offset  = $matches[0][1]+1;
}
echo "total=$total\n";

输出:

total=3

第二个例子的结果是: total=2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM