1

I am trying to learn regular expressions to validate specific inputs. The inputs I am trying to validate consist of the following patterns:

A regular expression to match the following pattern of phone numbers that might include a . or - or space. My data looks like this:

num: 1234567890 name: jack
num: 123-456-7890 name: john
num: 123.456.7890 name: jeff
num: 123 456 7890 name: josh
num: (123) 456-7890 name: jacob

A regular expression to match a social security number that might include dashes or spaces following the 3rd and 5th digit.

ss: 111111111 name: jack
ss: 111-11-1111 name: john
ss: 111 11 1111 name: jeff

I have tried \d{3}-?.?\d{3}-?.?\d{4} for the phone numbers.

And tried \d{3}-?\d{2}-?\d{4} for the social security numbers.

6
  • 1
    . must be escaped in a regular expression. Except if you put it in a character class. For example, the character class that separates the digits can be [.- ]. Commented Sep 10, 2013 at 15:07
  • 1
    @ArnaudDenoyelle: That's an invalid character class. Notice that the - has special meaning… Commented Sep 10, 2013 at 15:11
  • Why is it wrong? Look at smerny's answer. That is what (s)he used and it works. Commented Sep 10, 2013 at 15:15
  • @ArnaudDenoyelle, it makes a difference if it is between characters or not. If it is not between it won't need to be escaped. Commented Sep 10, 2013 at 15:16
  • 1
    I understand now. [ .-] is interpreted as [` ` or . or -] whereas [.- ] is interpreted as [between . and ` `]. Commented Sep 10, 2013 at 15:18

3 Answers 3

2

To match the portion of the phone numbers you could use..

\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}

Regular expression:

\(            # match '('
  ?           # match between zero and one time
  \d{3}       # match a digit (0-9) (3 times)
\)            # match ')'
  ?           # match between zero and one time
  [-. ]       # match for '-', '.', or ' '
  ?           # match between zero and one time
   \d{3}      # match a digit (0-9) (3 times)
   [-. ]      # match for '-', '.', ' '
  ?           # match between zero and one time
   \d{4}      # match a digit (0-9) (4 times)

And for the social security numbers, something like..

\d{3}[- ]?\d{2}[- ]?\d{4}
Sign up to request clarification or add additional context in comments.

Comments

1

For phone number, try this:

\(?\d{3}\)?[ .-]?\d{3}[ .-]?\d{4}

[ .-]? is basically saying "one or zero" of space, hyphen, or dot.

For ssn:

\d{3}[ -]?\d{2}[ -]?\d{4}

[ -]? is basically saying "one or zero" of space or hyphen.


But honestly rather than care about format (which might frustrate user and you - trying to account for various formats), in most cases I'd rather just make sure they have the right amount of numbers. You could remove any non-numeric character and then check the length.

How this is done would depend on the language being used. In javascript, it could look as simple as:

if(phoneNumber.replace(/\D/g, "").length() === 10){ //valid }

Then you could format or use it however you want from that point. Here is a javascript example of that: fiddle... an example using a specific format: fiddle

Comments

0

try this reg exp

(\(\+?\d+\) |\+?\d+)([ -.]?\d+)*

It looks like it works as required.

Comments