137

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

shA.t
  • 16,580
  • 5
  • 54
  • 111
fingerman
  • 2,440
  • 4
  • 19
  • 24
  • You can use [T-Regx library](https://github.com/Danon/T-Regx), that doesn't need delimiters. – Danon Oct 09 '18 at 13:54

2 Answers2

207

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

Kobi
  • 135,331
  • 41
  • 252
  • 292
  • 4
    For those who do not read linked materials, use `[` and `]` delimiters, otherwise you run into conflicts with the pattern itself. – greenoldman Feb 01 '16 at 13:08
26

Your regex pattern needs to be in delimiters:

$numpattern="/^([0-9]+)$/";
David Powers
  • 1,644
  • 12
  • 11