没有模式分隔符的正则表达式不会使用 preg_match() 发出警告
$first = ".";
$last = "a";
$middle= "testing-123.test";
if (preg_match("[a-zA-Z0-9]", $first)){
echo 'Frist Returned valid.';
}
if (preg_match("[a-zA-Z0-9.-]", $middle)){
echo 'Middle Returned valid.';
}
if (preg_match("[a-zA-Z0-9]", $last)){
echo 'Last Returned valid.';
}
我使用 substr 将字符串分解为 3 部分,因此在本例中原始文本为“.testing-123.testa”。我在这里没有包含 substr,只是预定义了本例中的变量。我想检查一下,这意味着以下要求:
第一个字符:az,AZ,0-9无符号
中间字符:az,AZ,0-9,仅。和 - 符号
最后一个字符:与第一个字符相同。
当我运行上面的代码时,它没有任何回显。有什么想法吗?
$first = ".";
$last = "a";
$middle= "testing-123.test";
if (preg_match("[a-zA-Z0-9]", $first)){
echo 'Frist Returned valid.';
}
if (preg_match("[a-zA-Z0-9.-]", $middle)){
echo 'Middle Returned valid.';
}
if (preg_match("[a-zA-Z0-9]", $last)){
echo 'Last Returned valid.';
}
I am breaking down a string into 3 parts using substr so in this example the original text was ".testing-123.testa". I didnt include the substr here but just predefined the variables in this case. I want to check so it means the following requirements:
First character: a-z, A-Z, 0-9 NO SYMBOLS
middle character: a-z, A-Z, 0-9, only . and - symbols
Last character: same as first character.
When I ran above code it echo'd nothing. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您缺少分隔符,请尝试在正则表达式周围添加
/
:您可能还想使用
preg_match_all
来查找更多/所有实例。You are missing delimitters, try adding
/
around your regex:You might also want to use
preg_match_all
to find more/all instances.应该做你需要的。
您需要分隔符(
/
),并且需要确保从字符串的开头到结尾测试所有字符(^
和$锚点)。
should do what you need.
You need delimiters (
/
), and you need to make sure that all characters are tested from the beginning to the end of the string (^
and$
anchors).无论您的编码意图如何,所有正则表达式模式都将左方括号和右方括号视为模式分隔符。发生这种情况是因为某些不同的字符可以用作模式分隔符。由于您的模式在技术上是有效的,因此不会发出警告来抱怨缺少分隔符。因此,您的字符类不会被视为字符类,而是被解释为主要包含文字字符的模式(仅
.
被视为元字符)。 展示a-zA-Z0-9
和a-zA-Z0-9 的演示#-
将被视为有效。 一个基本的解决方案是将模式括在正斜杠中。此外,您的模式不会验证整个字符串——它们仅检查是否有 1 个字符匹配。要验证完整字符串,您需要将字符串的开头与
^
匹配,需要一个或多个字符与+
匹配,然后将字符串的结尾与匹配>$。
最终,只有在没有更简单的解决方案时才应使用正则表达式。要简单地检查字符串是否完全由字母数字字符组成,最理想的调用是
ctype_alnum()
。推荐解决方案:(演示)
输出:
Despite your coding intention, all of your regex patterns are treating the opening square brace and the closing square brace as pattern delimiters. This is happening because some non-identical characters can be used as pattern delimiters. Because your pattern is technically valid, no Warning is emitted to complain about missing delimiters. As a consequence, your character class is not treated as a characters class and is instead interpreted as a pattern containing mostly literal characters (only the
.
is treated as a metacharacter). Demo showing thata-zA-Z0-9
anda-zA-Z0-9#-
will be deemed valid. A basic solution is to wrap your patterns in forward slashes.Also, your patterns do not validate the entire string -- they only check if as little as 1 character is matched. To validate the full string, you need to match the start of the string with
^
, require one or more characters with+
, then match the end of the string with$
.Ultimately, regex should only be used when there isn't a simpler solution. To simply check if a string is completely comprised of alphanumeric characters, the most ideal call is
ctype_alnum()
.Recommended solution: (Demo)
Output: