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


I want to validate following as valid card number


But anything extra then this should not match.


That is these should not match.

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



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


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


Leave an answer

Quote of the day: live life