Welcome to MSDN Blogs Sign in | Join | Help

Regular Expression Exercise S1

The first in a series of exercises designed to teach you more about regular expressions, written by a guy who got partway through writing a regex book.

But first, a word about tools. It's a lot easier to use a tool to do this sort of thing than it is to write code to do it. So, I suggest one of the following:

So, S stands for simple, 1 stands for 1, so this first one is going to be pretty simple.

S1 - Match a Social Security Number

Verify that a string is a social security number of the format ddd-dd-dddd.

 

Answer and explanation to follow.

Published Friday, October 21, 2005 12:33 PM by ericgu
Filed under:

Comments

Friday, October 21, 2005 4:23 PM by Nat Luengnaruemitchai

# re: Regular Expression Exercise S1

^\d{3}-\d{2}-\d{4}$
Friday, October 21, 2005 11:20 PM by Dvae

# re: Regular Expression Exercise S1

Surely you'd want the hypens to be optional or replaceable by spaces. Forcing a particlular format when enter a number like this, or a credit-card number can be a pain, unless you explictly tell the user in advance the format you want (as you have done).

In any case, I'd use
^\d{3}[-| ]?\d{2}[-| ]?\d{4}$
Monday, October 24, 2005 6:41 AM by David

# re: Regular Expression Exercise S1

The best tool one can have when working with regular expressions is Regex Coach. http://weitz.de/regex-coach/
Monday, October 24, 2005 11:01 AM by James Curran

# re: Regular Expression Exercise S1

Dvae:
Yes, but you'd want consistency in the separator use, so that it would accept "123-12-1234", "123 12 1234" or "123121234" but not "12312 1234" or "123-12 1234". Hence we'd need:

^\d{3}(?<Sep>[- ]?)\d{2}\<Sep>\d{4}$


(as interpreted by Regex Workbench)

^ (anchor to start of string)
Any digit
Exactly 3 times
Capture to <Sep>
Any character in "- "
? (zero or one time)
End Capture
Any digit
Exactly 2 times
<Sep>
Any digit
Exactly 4 times
$ (anchor to end of string)

Monday, October 24, 2005 3:53 PM by Dave

# re: Regular Expression Exercise S1

> Yes, but you'd want consistency in the separator use

True, but if we wanted consistency, then we'd force them to use the properly structured format, and not allow for any variance ;-)

<strike>Dvae</strike> Dave
Friday, October 28, 2005 1:22 AM by Richard

# re: Regular Expression Exercise S1

Why would I want to match one of those? Completely useless outside the US.
Friday, October 28, 2005 1:34 PM by James Curran

# re: Regular Expression Exercise S1

Um... So, RIchard, what exactly is the format of a Social Security Number outside the US?
Sunday, November 06, 2005 1:33 PM by Rob

# re: Regular Expression Exercise S1

In the UK it is

AA 00 00 00 A

or

^\w{2} \d{2} \d{2} \d{2} \w$

# Eric Gunnerson s C Compendium Regular Expression Exercise S1 | Wood TV Stand

# Eric Gunnerson s C Compendium Regular Expression Exercise S1 | Green Tea Fat Burner

# Eric Gunnerson s C Compendium Regular Expression Exercise S1 | pool toys

New Comments to this post are disabled
 
Page view tracker