user1863261
user1863261

Reputation: 107

regular expression for numbers with many spaces and special characters?

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

Answers (3)

Grhm
Grhm

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 times

Upvotes: 1

Hans Kesting
Hans Kesting

Reputation: 39358

To allow any number of spaces, dashes and #-signs between up to 12 numbers, use this:

^[ #-]*([0-9][ #-]*){0,12}$
  • initially 0 or more specials
  • then 0-12 groups of
    • one digit
    • followed by 0 or more specials

Upvotes: 3

John Dvorak
John Dvorak

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

Related Questions