user1818573
user1818573

Reputation: 45

Regex Matching Issue

I want to validate an input of format "AB1234" where first two characters must be Alpha (A-Z) and remaining must be numbers.
My current regex is validating the input "AB123A" which is incorrect. What is missing my current regex?

^[a-zA-Z]{2}\d{1,6}

Upvotes: 2

Views: 60

Answers (3)

Anirudha
Anirudha

Reputation: 32817

You are missing $ which specifies the end of the string

^[a-zA-Z]{2}\d{1,6}$

^[a-zA-Z]{2}\d{1,6} without $ matches AB123A since you are not specifying any end to that string..

It matches AB123 within AB123A

Upvotes: 4

user1498339
user1498339

Reputation: 629

Add a $ dollar to the end of your regexp:

^[a-zA-Z]{2}\d{1,6}$

otherwise it matches the "AB123" part of "AB123A".

Upvotes: 0

GarethL
GarethL

Reputation: 1493

You are missing a $ at the end:

^[a-zA-Z]{2}\d{1,6}$

The $ specifies the end of the string being tested.

Upvotes: 2

Related Questions