Viewed   68 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

2

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

Tuesday, September 6, 2022
5
<?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.

Monday, November 21, 2022
3

If a simple pattern would be sufficient depends on how your input could look like.

$re = '~Q$data['infos'][]E.*?);~s';
  • Q...E is used to match literally (also could escape the brackets/dollar).
  • .*? in single line mode (s flag) matches lazily any amount of any character.

See demo at regex101 or php demo at eval.in

Sunday, October 30, 2022
4

When you set the validation rules you separate them with a | so the |'s in your regex is causing the validation rule to split at those and that is causing the error. Discussion here on the issue. It seems it's a limitation or bug in codeigniter. You can test it out by running a regex with and without |'s and see if the usage of pipes will cause an error. If that is the case then you may have to validate by regex by other means, maybe use a callback function as detailed on this page where your function will do a preg_match using the regex which needs to be inside the function of course and then return true/false.

Saturday, December 3, 2022
 
4

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.

Sunday, October 23, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :