PHP 正则表达式:未找到结束分隔符 '^'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4634993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:46:37  来源:igfitidea点击:

PHP regular expressions: No ending delimiter '^' found in

phpregexpreg-matchpcre

提问by fingerman

I've been having some trouble with regular expressions.

我在使用正则表达式时遇到了一些麻烦。

This is my code

这是我的代码

$pattern = "^([0-9]+)$";

if (preg_match($pattern, $input))
   echo "yes";
else
   echo "nope";

I run it and get:

我运行它并得到:

Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in

警告:preg_match() [function.preg-match]:在

回答by Kobi

PHP regex strings need delimiters. Try:

PHP 正则表达式字符串需要分隔符。尝试:

$numpattern="/^([0-9]+)$/";

Also, note that you have a lower case o, not a zero. In addition, if you're just validating, you don't need the capturing group, and can simplify the regex to /^\d+$/.

另外,请注意您使用的是小写的 o,而不是零。另外,如果只是验证,则不需要捕获组,可以将正则表达式简化为/^\d+$/.

Example: http://ideone.com/Ec3zh

示例:http: //ideone.com/Ec3zh

See also: PHP - Delimiters

另请参阅:PHP - 分隔符

回答by David Powers

Your regex pattern needs to be in delimiters:

您的正则表达式模式需要在分隔符中:

$numpattern="/^([0-9]+)$/";

回答by Danon

You can use T-Regx library, that doesn't need delimiters

您可以使用不需要分隔符的T-Regx 库

pattern('^([0-9]+)$')->match($input);