Regular Expression for SSN
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
Regular Expression for SSN
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
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
Regular Expression for SSN
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]
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]
-
- Posts: 2461
- Joined: Sun Mar 02, 2003 9:22 pm
-
- Posts: 2461
- Joined: Sun Mar 02, 2003 9:22 pm
-
- Posts: 59
- Joined: Fri Apr 18, 2008 3:25 am
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.
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."