0

Could you please help me to provide a RegEx pattern to validate a string which satisfy:

  1. length from 4 to 10 (strictly)
  2. first 3 characters must be string (A-Z a-z)
  3. the remain characters must be number without 00 as prefix, I mean ABC15 is passed but ABC0015 is not.

This problem took me so much time and I have tried so many regex patterns, but I still have no solution for it. Thank you so much.

1
  • 1
    Can you provide some examples you tried? Commented Feb 19, 2012 at 18:20

2 Answers 2

4

Use this one:

/^[a-z]{3}(?!00)\d{1,7}$/i

Explanation:

/
^         Start
[a-z]{3}  Three letters.
(?!00)    Must NOT be followed by two zeros.
\d{1,7}   One to seven digits.
$         End.
/i        ignore case flag.
Sign up to request clarification or add additional context in comments.

Comments

1

Easy.

/^[a-z]{3}[1-9][0-9]{0,6}$/i

Matches three letters (case insensitive flag at the end), followed by one digit that is not zero, followed by up to six more digits (which may be zero).

3 Comments

No it isn't. It's (3 letters) + (1 digit) + (up to 6 digits), which has 4 as a minimum and 10 as a max.
This pattern also matches AAA4444444444444444444, because of the missing ^ and $. Also, it allows consecutive zeros ater [a-z]{3}.
Whoopsie. Usually I don't forget things like that. And I wasn't sure if it should allow ABC0123 (ie. one leading zero) or not... The question didn't really clear that one up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.