Regular Expression for SSN

General questions about using TextPad

Moderators: AmigoJack, helios, bbadmin, Bob Hansen, MudGuard

Post Reply
SZY8XJ
Posts: 7
Joined: Tue Jul 13, 2004 1:49 pm

Regular Expression for SSN

Post 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
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post by MudGuard »

\d is not available in Textpad's regex engine. use [0-9] instead.
SZY8XJ
Posts: 7
Joined: Tue Jul 13, 2004 1:49 pm

Post by SZY8XJ »

so...

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

doesn't work either - did I get the syntax wrong?
scrubbie
Posts: 1
Joined: Fri Aug 13, 2010 9:31 pm

Regular Expression for SSN

Post 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]
ben_josephs
Posts: 2456
Joined: Sun Mar 02, 2003 9:22 pm

Post 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
SZY8XJ
Posts: 7
Joined: Tue Jul 13, 2004 1:49 pm

Post 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!
SZY8XJ
Posts: 7
Joined: Tue Jul 13, 2004 1:49 pm

Post 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!
ben_josephs
Posts: 2456
Joined: Sun Mar 02, 2003 9:22 pm

Post 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
SZY8XJ
Posts: 7
Joined: Tue Jul 13, 2004 1:49 pm

Post by SZY8XJ »

Perfect - thank you!
HerNameWasTextPad
Posts: 59
Joined: Fri Apr 18, 2008 3:25 am

Post 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.
I came in on 4.5 in 2001, moved to 4.7.2 in 2004, moved to 4.7.3 in 2007, moved to 5.4 in 2010, and am excited about 2013. I've said it many times before, and I'll say it many times again: "I love this program."
Post Reply