匹配,在某个单词之前没有一个单词

时间:2016-05-07 23:49:06

标签: php regex preg-match

我想匹配“老鼠”这个词。在它之前没有任何言语,但我的正则表达式不起作用。我究竟做错了什么?

<?php

$string1 = "one mice";
$string2 = "one .mice";
$string3 = "mice";

if (preg_match("~(?<![a-z]+ )\bmice~", $string1)) { echo "1"; }
if (preg_match("~(?<![a-z]+ )\bmice~", $string2)) { echo "2"; }
if (preg_match("~(?<![a-z]+ )\bmice~", $string3)) { echo "3"; }

?>

预期结果:

23

实际结果:

我希望它能回应2,因为它不是在鼠标之前有一个词,而是一个句号。而且我希望它能回应3,因为在“老鼠”这个词之前没有任何字眼。我做错了什么?

3 个答案:

答案 0 :(得分:1)

您可以使用negative lookbehind并使dot成为可选项,即:

if (preg_match('/(?<![a-z] )\.?\bmice\b/i', $subject)) {
    # Successful match
}

正则表达式解释:

(?<![a-z] )\.?\bmice\b

Options: Case insensitive

Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<![a-z] )»
   Match a single character in the range between “a” and “z” (case insensitive) «[a-z]»
   Match the character “ ” literally « »
Match the character “.” literally «\.?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Assert position at a word boundary (position preceded or followed—but not both—by a Unicode letter, digit, or underscore) «\b»
Match the character string “mice” literally (case insensitive) «mice»
Assert position at a word boundary (position preceded or followed—but not both—by a Unicode letter, digit, or underscore) «\b»

Regex101 Demo

Ideone Demo

注意:

  1. PHP preg&gt; = 5.6.11 不支持lookbehind中的无限重复 (?<![a-z]+ )

答案 1 :(得分:0)

$string1 = "one mice";
$string2 = "one .mice";
$string3 = "mice";
$regex = "/([^A-z ]|^)mice/";

if (preg_match($regex, $string1)) { echo "1"; }
if (preg_match($regex, $string2)) { echo "2"; }
if (preg_match($regex, $string3)) { echo "3"; }

答案 2 :(得分:0)

这种(*SKIP)(*FAIL)技术将扮演一个“非固定宽度负面后视”的角色,并且在我的6弦测试案例中,与Pedro(我的133与他的221)相比,所需的步骤更少 - 提供相同的结果 - 请参阅Pattern Demo

模式:/[a-z]+ mice(*SKIP)(*FAIL)|\bmice\b/

代码:(Demo

$strings=[1=>"one mice",2=>"one .mice",3=>"mice"];
foreach($strings as $i=>$string){
    if(preg_match('/[a-z]+ mice(*SKIP)(*FAIL)|\bmice\b/',$string)){ echo $i;}
}
// output: 23