Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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 为什么部分匹配时preg_match()总是验证为true?_Php_Regex - Fatal编程技术网

Php 为什么部分匹配时preg_match()总是验证为true?

Php 为什么部分匹配时preg_match()总是验证为true?,php,regex,Php,Regex,最近我一直在用正则表达式进行解释,当我试图确认preg_match()函数没有返回预期结果(false)时。我意识到我的正则表达式在部分匹配和完全匹配的情况下都是正确的 有经验的人能分享一下为什么会这样吗 我已使用以下代码对此进行了测试: <?php # Storing regexp. $pattern = "/Banana|Apples/i"; # Storing value that will be compared against regexp. $value = "Chiiii

最近我一直在用正则表达式进行解释,当我试图确认
preg_match()
函数没有返回预期结果(
false
)时。我意识到我的正则表达式在部分匹配和完全匹配的情况下都是正确的

有经验的人能分享一下为什么会这样吗

我已使用以下代码对此进行了测试:

<?php

# Storing regexp.
$pattern = "/Banana|Apples/i";

# Storing value that will be compared against regexp.
$value = "Chiiiiiiiibanana";

# Testing how preg_match is dealing with the regexp.
if (preg_match($pattern, $value, $matches)) {
    echo "It's a match!\n";

    # In case there's matches, print them.
    print_r($matches);
} else {
    echo "Sorry, not a match.";
}

?>

要避免任何部分匹配,必须使用锚定:

  • ^
    -字符串的开头
  • $
    -字符串结束
所以使用

$pattern = "/^(?:Banana|Apples)$/i";

因为您有一个备选列表,所以需要对它们进行分组,以便锚定正确应用,而不仅仅是第一个和最后一个备选。如果您使用
“/^Banana | Apples$/i”
香蕉
将在
香蕉
苹果
中匹配


要仅对备选方案进行分组而不存储在任何捕获组中,可以使用非捕获组(
(?:…)
)。此外,您不需要在整个模式上设置捕获组,因为整个匹配文本始终存储在组0中。

字符串的开头是
^
您必须查找锚定:
^
$
<代码>$pattern=“/^(?:香蕉|苹果)$/i”查看更多关于为什么我应该使用anchor的详细信息(我会在后面进一步研究),以及为什么使用anchor?元字符,如果前面没有任何内容?谢谢。您只是说此字符串是否包含香蕉。确实如此。使用锚点,你说这个字符串是否只包含香蕉(或苹果)。你可能还想在苹果上选择
s
。刚刚测试了你的regexp,但是因为我想了解发生了什么,所以我仍然希望了解更多。我会多读一点。非常感谢@Stribizhev回答得既好又翔实。然而,一段时间后,表意文字链接往往会消失(这是我最近学到的)。如果该链接不再可用,最好(如果您愿意的话)包含该链接的实际代码。@Fred ii-:我知道,但代码已经在这篇文章中:OP的代码和我的修改工作正常。没有问题。只是一个“旁注”;-)