PHP regular expression for a positive number with 0 or 2 decimal places

I am trying to use the following regex to check if a string is a positive decimal number or 2:

^\d+(\.(\d{2}))?$

When I try to match this using preg_match, I get an error:

Warning: preg_match (): trailing delimiter '^' found in /Library/WebServer/Documents/lib/forms.php on line 862

What am I doing wrong?

+3
source share
2 answers

The error is that there is no delimiter /or #etc., make sure you enclose the regex expression in delimiters:

if (preg_match('/^\d+(\.(\d{2}))?$/', $text))
{
 // more code
 return true;
}

Or:

if (preg_match('#^\d+(\.(\d{2}))?$#', $text))
{
 // more code
 return true;
}
+2
source

PHP preg , /; , /[a-z]/ a z. "^\\d+(.(\\d{2}))?$", \\d+(.(\\d{2}))?$, ^ s, ^. , , , "/^\\d+(.(\\d{2}))?$/". , , - .; , , ; \.. "/^\d+(\.(\d{2}))?$/". , , "/^\d+(?:\.(\d{2}))?$/", $1 $2.

+1

Source: https://habr.com/ru/post/1749711/


All Articles