PHP regular expressions: No ending delimiter '

2018-12-31 15:26发布

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

3条回答
梦该遗忘
2楼-- · 2018-12-31 15:29

PHP regex strings need delimiters. Try:

$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+$/.

Example: http://ideone.com/Ec3zh

See also: PHP - Delimiters

查看更多
不再属于我。
3楼-- · 2018-12-31 15:34

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

pattern('^([0-9]+)$')->match($input);
查看更多
回忆,回不去的记忆
4楼-- · 2018-12-31 15:37

Your regex pattern needs to be in delimiters:

$numpattern="/^([0-9]+)$/";
查看更多
登录 后发表回答