Removing Characters and Replacing Numbers Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Removing Characters and Replacing Numbers

Post by jarhead » 24 Sep 2020, 15:15

So I have a number that will always contain 9 digits (social security number) in the typical SSN format: 123-45-6789

I have the following script that removes any spaces or dashes in the number that I have copied to the clipboard and pastes it as 123456789:

Code: Select all

^g::
Clipboard := StrReplace(Clipboard, "-", "")
Clipboard := StrReplace(Clipboard, " ", "")
Send ^v
return
First question... is there a way to combine the two StrReplace lines or is this the best way to do what I'm wanting?

Next, I want to have another script that does the same as the above and after removing any spaces and dashes, replaces the first five digits with an X so I get XXXXX6789 and places on the clipboard where I can paste. I've been playing with the following script that removes the first 5 characters but I'm not sure how to insert an X five times:

Code: Select all

Clipboard := SubStr(Clipboard, 6, 4)
return
I've played with regex trying to figure out the best way to do so but I have a feeling I'm making it harder than it is. Any help is greatly appreciated.

User avatar
Yakshongas
Posts: 590
Joined: 21 Jan 2020, 08:41

Re: Removing Characters and Replacing Numbers  Topic is solved

Post by Yakshongas » 24 Sep 2020, 15:28

Code: Select all

SSN := "123-45-6789"

^g::
Clipboard := StrReplace(Clipboard, " ", "")
Clipboard := StrReplace(Clipboard, "-", "")
Clipboard := RegExReplace(Clipboard, "\d{5}", "XXXXX")
Send, ^v
Return
Please mark your topics as solved if you don't need any further help. ✅

Need a little more help? Discord : Yakshongas#9893 🕹

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Removing Characters and Replacing Numbers

Post by Xtra » 24 Sep 2020, 15:28

Code: Select all

clipboard := "123-45-6789"
Clipboard := "XXXXX" . SubStr(StrReplace(StrReplace(Clipboard, "-"), " "), 6, 4)    ; XXXXX6789

jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Re: Removing Characters and Replacing Numbers

Post by jarhead » 24 Sep 2020, 15:35

Yakshongas wrote:
24 Sep 2020, 15:28

Code: Select all

SSN := "123-45-6789"

^g::
Clipboard := StrReplace(Clipboard, " ", "")
Clipboard := StrReplace(Clipboard, "-", "")
Clipboard := RegExReplace(Clipboard, "\d{5}", "XXXXX")
Send, ^v
Return
Easy enough. Works great! Thanks.

User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: Removing Characters and Replacing Numbers

Post by boiler » 24 Sep 2020, 15:45

Using RegEx only:

Code: Select all

^g::
Clipboard := RegExReplace(Clipboard, "\d{3}[- ]\d{2}[- ](\d{4})", "XXXXX$1")
Send ^v
return

User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: Removing Characters and Replacing Numbers

Post by boiler » 24 Sep 2020, 15:53

To just replace the dashes or spaces in one line:

Code: Select all

^g::
Clipboard := RegExReplace(Clipboard, "[- ]")
Send ^v
return

jarhead
Posts: 151
Joined: 09 Sep 2020, 12:43

Re: Removing Characters and Replacing Numbers

Post by jarhead » 24 Sep 2020, 16:05

boiler wrote:
24 Sep 2020, 15:45
Using RegEx only:

Code: Select all

^g::
Clipboard := RegExReplace(Clipboard, "\d{3}[- ]\d{2}[- ](\d{4})", "XXXXX$1")
Send ^v
return
Thanks! Trying to learn more about regex and this helps.

Post Reply

Return to “Ask for Help (v1)”