Benzon
Benzon

Reputation: 129

Correct regex for this pattern

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?

Upvotes: 1

Views: 34

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59601

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.

Upvotes: 1

Related Questions