Asked  7 Months ago    Answers:  5   Viewed   30 times

I've been having some trouble with regular expressions.

This is my code

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

if (preg_match($pattern, $input))
   echo "yes";
   echo "nope";

I run it and get:

Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in



PHP regex strings need delimiters. Try:


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


See also: PHP - Delimiters

Wednesday, March 31, 2021
answered 7 Months ago

var_dump( preg_match( $regex, 'M1 1AA' ), preg_match( $regex, 'not valid' ) );

Works like a charm for me. You need to have delimiters in place.

Wednesday, March 31, 2021
answered 7 Months ago

The reason why this happens is the different line break styles at regex101 (n) and in your input (rn).

You can easily solve this by using a unified R pattern for any kind of linebreaks.

Note I did not optimize your pattern, I am just showing how to solve the problem stated in the question:

'~[0-9]+s{0,10}R?([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}),([0-9]{1,3}) --> ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}),([0-9]{1,3})s{0,10}R(.*R?.*R?.*)R{2}~'

See the PHP demo

Wednesday, March 31, 2021
answered 7 Months ago

Did you try the (*CRLF) and related modifiers? They are detailed on Wikipedia here (under Newline/linebreak options) and seem to do the right thing in my testing. i.e. '/(*CRLF)^two$/m' should match the windows rn newlines. Also (*ANYCRLF) should match both linux and windows but I haven't tested this.

Saturday, May 29, 2021
answered 5 Months ago

Like Gumbo said, preg_match is not binary safe.

Use instead:

preg_match("/^([^\x{00}-\x{1F}]+?){0,1}/", 'test string'));

This is the correct way to specify Unicode code points in PCRE.

Saturday, May 29, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :