Reputation: 375
I'm trying to get a reg. Expression to match a pattern of exactly 11 digits OR 0 to 10 digits padded to 11 characters w/ spaces (\x20).
Match: “12345678901”, “ “, “123 “
Don't Match: “ 5678901”, “123 78901”, “123 789 ”
The expression '/((\d)|(\x20)){11}/'
matches the desired strings however it also still matches strings that have leading and internal spaces. I've played w/ various combinations of the leading / trailing anchors (^,$) but can't seem to get the right result. Any help will be much appreciated.
Upvotes: 2
Views: 226
Reputation: 784998
You can use this lookahead based regex:
^(?=\d* *$)[\d ]{11}$
Upvotes: 3
Reputation: 8197
I recommend you to test this condition in your code, cause regex will be too ugly.
\d{11}|\s\d{10}|\s{2}\d{9}|\s{3}\d{8}|\s{4}\d{7}|\s{5}\d{6}|\s{6}\d{5}|\s{7}\d{4}|\s{8}\d{3}|\s{9}\d{2}|\s{10}\d|\s{11}
Upvotes: 1