PHP regular expressions: No ending delimiter '^' found in
130,793
Solution 1
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
Solution 2
Your regex pattern needs to be in delimiters:
$numpattern="/^([0-9]+)$/";
Solution 3
You can use T-Regx library, that doesn't need delimiters
pattern('^([0-9]+)$')->match($input);
Author by
fingerman
Updated on October 10, 2020Comments
-
fingerman over 3 years
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
-
greenoldman over 8 yearsFor those who do not read linked materials, use
[
and]
delimiters, otherwise you run into conflicts with the pattern itself. -
MartinNajemi almost 3 yearsWhy would you use an additional library? Just encase the regex in '/'
-
Danon almost 3 years@MartinNajemi It's just a suggestion. The first accepted answer suggests enclosing with delimiters "/", and my answer suggests using an alternative. There's no point in posting two similar answers.