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.
.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[.- ].-has special meaning…[ .-]is interpreted as [` ` or.or-] whereas[.- ]is interpreted as [between.and ` `].