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";
else
   echo "nope";

I run it and get:

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

 Answers

73

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

Wednesday, March 31, 2021
 
fillobotto
answered 7 Months ago
53
<?php
$regex = '/(((^[BEGLMNS][1-9]d?)|(^W[2-9])|(^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)dd?)|(^W1[A-HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY]))(s*)?([0-9][ABD-HJLNP-UW-Z]{2}))$|(^GIRs?0AA$)/';

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
 
PeterTheLobster
answered 7 Months ago
63

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
 
eek
answered 7 Months ago
eek
69

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
 
employeegts
answered 5 Months ago
100

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
 
SilverHorn
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 :