Home Ask Login Register

Developers Planet

Your answer is one click away!

TBAG February 2016

Regex is not matching exact length of string

I have following regex to validate card number

[\d+]{6,6}[X|x]{8,8}[\d+]{4,4}

I want to validate following as valid card number

 123456XXXXxxxx1234

But anything extra then this should not match.

 123456XXXXxxxx1234a
bbb123456XXXXxxxx1234nnn

That is these should not match.

I tried this regular expression but it is not working for me.

^[\d+]{6,6}[X|x]{8,8}[\d+]{4,4}$

Answers


Wiktor Stribi┼╝ew February 2016

Your [\d+]{4,4} matches 4 digits or + symbols. I think you just meant \d{4} (4 digits). Also, [X|x]{8,8} matches 8 X, x or | symbols. I guess you need just [Xx]{8}.

I believe you need

^\d{6}[Xx]{8}\d{4}$

See regex demo

If you have time, you might want to read more about Character Classes or Character Sets.

Post Status

Asked in February 2016
Viewed 1,666 times
Voted 11
Answered 1 times

Search




Leave an answer


Quote of the day: live life