[英]Preg_match() usage with “{}” and “?”
I'm currently trying things with reggae (PCRE) and PhP. 我目前正在尝试使用雷鬼(PCRE)和PhP。
It seems that the function pre_match() does not work properly with the signs {} and ? 似乎函数pre_match()无法使用符号{}和? and I'm wondering why.
而且我想知道为什么。 Can someone help me?
有人能帮我吗?
Here is the code I'm using just for the example: 以下是我仅用于示例的代码:
<?php
$search = "aaaah";
//Return "valid" while I ask for 3 "a" specifically
if(preg_match("&a{3}&", $search)){
echo 'Valid <br/>';
}else{
echo 'Invalid <br/>';
}
//Also return "valid" while I ask for 0 or 1 "a"
if(preg_match("&a?&", $search)){
echo 'Valid <br/>';
}else{
echo 'Invalid <br/>';
}
?>
There must be something I misunderstood. 必须有一些我误解的东西。 Can someone explain?
谁能解释一下? I did not find anything anywhere.
我什么都没找到。 Thank you in advance!
先感谢您!
I'm using MAMP with a Mac and coding in utf-8. 我在Mac上使用MAMP并在utf-8中编码。
The results you get are expected as: 您获得的结果预计为:
a
's in a row and in your example there are two of those series possible, the first 3 a
's and the last 3 a
's. a
,在你的例子中有两个系列可能,前三个a
和后三个a
。 So that is a match as you don't specify what has to happen before or after your series of 3 a
's. a
的。 a
character. a
角色。 Any input will match that as you are looking for 0 or 1 a's and that will always be true for everything you feed it, whether it contains a
's or not. a
的或没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.