Page 1 of 1

Regular Expression for SSN

Posted: Fri Aug 13, 2010 2:59 pm
by SZY8XJ
Hello -

I am trying to find all SSN's (nnn-nn-nnnn) and replace them with XSSN (X999-99-9999).
The regular expression I am trying to use to find them is ^\d{3}-?\d{2}-?\d{4}$ and it isn't working.

Can someone tell me what you use successfully?

Many thanks!
steve

Posted: Fri Aug 13, 2010 3:36 pm
by MudGuard
\d is not available in Textpad's regex engine. use [0-9] instead.

Posted: Fri Aug 13, 2010 4:53 pm
by SZY8XJ
so...

^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$

doesn't work either - did I get the syntax wrong?

Regular Expression for SSN

Posted: Fri Aug 13, 2010 9:47 pm
by scrubbie
Your regex is anchored with the '^' and '$' characters.

Unless you are expecting the SSN to appear by itself on a single line with no whitespace this won't work. Try removing the anchors?

Otherwise it may be that TextPad doesn't support intervals, {x,y}, either. Try:
[0-9][0-9][0-9]-?[0-9][0-9]-?[0-9][0-9][0-9][0-9]

Posted: Fri Aug 13, 2010 11:20 pm
by ben_josephs
TextPad's regex recogniser does support bounded repetition ("intervals"). To use the {...} syntax without backslashes, use Posix regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax

Posted: Mon Aug 16, 2010 1:13 pm
by SZY8XJ
[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9] works without the ? after the -
Thanks for everyone's help!

Posted: Wed Aug 18, 2010 6:08 pm
by SZY8XJ
Can anyone extrapolate the answers above into a technique to find all of the SSN's (as discussed above) and add a new line \n character in front of them?

Thanks!

Posted: Wed Aug 18, 2010 7:36 pm
by ben_josephs
Find what: [0-9]{3}-[0-9]{2}-[0-9]{4}
Replace with: \n\0

[X] Regular expression

Replace All
This assumes you are using Posix regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax

Posted: Wed Aug 18, 2010 8:09 pm
by SZY8XJ
Perfect - thank you!

Posted: Mon Nov 29, 2010 11:00 pm
by HerNameWasTextPad
If you're like me, and you prefer the TextPad regular-expression syntax over that of POSIX, then try this, using the Replace dialog box with "Regular expression" and "Match case" checked. Don't forget to uncheck "Use POSIX . . ." in Editor Preferences:

Replace \([0-9]\{3\}-*[0-9]\{2\}-*[0-9]\{4\}\) by X\1 and nothing else.

This assumes that the "X" is an X character--and not a variable.

If all of the SSNs in the list begin at the left margin, then you can use this one, which additionally adds a new line above each Xnnn-nn-nnnn:

Replace ^\([0-9]\{3\}-*[0-9]\{2\}-*[0-9]\{4\}\) by \nX\1 and nothing else.