1

I've got some issues understanding this regex.

I tried doing a pattern but does not work like intended.

What I want is [A-Za-z]{2,3}[0-9]{2,30}

That is 2-3 letters in the beginning and 2-30 numbers after that

FA1321321
BFA18098097

I want to use it to validate an input field but can't figure out how the regex should look like.

Can any one that can help me out even explain a bit about it?

2
  • 1
    Your regex seems fine to me Commented Jan 26, 2016 at 4:18
  • Se answer below. If you don't anchor it to start and end of string then you could still match the portion in the middle of the string that fits your rule. For example something like ABCDE54321 begins a match at C. Commented Jan 26, 2016 at 4:31

1 Answer 1

1

Your regex is correct - just make sure to surround it with / in PHP, and perhaps ^, $ if you want it to strictly match the entire string (no extra characters before/after).

$pattern = "/^[A-Za-z]{2,3}[0-9]{2,30}$/"
$found = preg_match($pattern, $your_str);

From the PHP documentation:

preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant regex is a tough cookie sometimes hehe, adleest i was close so thats a good thing. And thanks for the help now i can finish up my code after severel hours of being mad ad regex :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.