Reputation: 107
Hi I am new to the regular expression.
Can someone tell how to format a regular expression pattern for validating Numbers with many spaces and special characters? here the space position are not defined.
I have tried like this ^[0-9]{0,12}$
but I don't know how to place the spaces in-between ?
Ex:'2356 85 568#','5875 #2545','#2525','4567'
Upvotes: 0
Views: 1493
Reputation: 6864
To allow "spaces and special characters" to include any non-digit characters, use:
^\D*(\d\D*){0,12}$
To explain:
^\D*
matches any non-digit characters at the start of the string(\d\D*)
matches a single digit followed by any non-digits{0,12}
allow the previous group to occur up to 12 timesUpvotes: 1
Reputation: 39358
To allow any number of spaces, dashes and #-signs between up to 12 numbers, use this:
^[ #-]*([0-9][ #-]*){0,12}$
Upvotes: 3
Reputation: 27317
If you want at most twelve digits, and an unlimited amount of whitespace and hash signs, you can do this:
^[\s#]*([0-9][\s#]*){0,12}$
Upvotes: 1