I am trying to validate a string of 3 numbers followed by / then 5 more numbers
I thought this would work
(/^([0-9]+[0-9]+[0-9]+/[0-9]+[0-9]+[0-9]+[0-9]+[0-9])/i)
but it doesn’t, any ideas what i’m doing wrong
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First of all, you’re using
/as the regexp delimiter, so you can’t use it in the pattern without escaping it with a backslash. Otherwise, PHP will think that you’re pattern ends at the/in the middle (you can see that even StackOverflow’s syntax highlighting thinks so).Second, the
+is “greedy”, and will match as many characters as it can, so the first[0-9]+would match the first 3 numbers in one go, leaving nothing for the next two to match.Third, there’s no need to use
i, since you’re dealing with numbers which aren’t upper- or lowercase, so case-sensitivity is a moot point.Try this instead
The
\dis shorthand for writing[0-9], and the{3}and{5}means repeat 3 or 5 times, respectively.(This pattern is anchored to the start and the end of the string. Your pattern was only anchored to the beginning, and if that was on purpose, the remove the
$from my pattern)