PHP正则表达式:没有分隔符“^”中结束

时间:2021-08-10 22:06:43

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()函数。preg-match]:没有分隔符“^”中结束

2 个解决方案

#1


99  

PHP regex strings need delimiters. Try:

PHP regex字符串需要分隔符。试一试:

$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,而不是0。此外,如果你只是验证,您不需要捕获组,可以简化的正则表达式/ ^ \ d + /美元。

Example: http://ideone.com/Ec3zh

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

See also: PHP - Delimiters

参见:PHP -分隔符

#2


15  

Your regex pattern needs to be in delimiters:

您的regex模式需要使用分隔符:

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

#1


99  

PHP regex strings need delimiters. Try:

PHP regex字符串需要分隔符。试一试:

$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,而不是0。此外,如果你只是验证,您不需要捕获组,可以简化的正则表达式/ ^ \ d + /美元。

Example: http://ideone.com/Ec3zh

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

See also: PHP - Delimiters

参见:PHP -分隔符

#2


15  

Your regex pattern needs to be in delimiters:

您的regex模式需要使用分隔符:

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