Hello,
There are many possible 10-digits phone number formats for North America.
1. +1 111 111 1111
2. +1-111-111-1111
3. +1 (111) 111-1111
4. 1 111 111 1111
5. 1-111-111-1111
6. 1-(111) 111-1111
7. 1111111111
8. 111.111.1111
9. 111-111-1111
etc.
I'm looking for code that will parse the phone number to 111.111.1111 as standard format, which also includes the removal of country code (+1) at front.
Ideally I'd like to be able to:
1) highlight the text using mouse e.g. +1 (111) 111-1111
2) activate code (see Code Steps)
3) paste as 111.111.1111
Code Steps
1) copy highlighted text to clipboard
2) look at stored value in clipboard
3) parse
4) save new value to clipboard
Is AHK able to do this?
Thanks,
Devin
Telephone number parser
-
- Posts: 1259
- Joined: 16 Apr 2015, 09:41
Re: Telephone number parser
AHK can definitely do that...
Code: Select all
f12::
CurrentClip := ClipboardAll
Clipboard =
Send ^c
ClipWait
Clipboard := RegExReplace(Clipboard, "^.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})$", "$1.$2.$3", 1)
Sleep 25
Send ^v
Sleep 250
Clipboard := CurrentClip
Return
Re: Telephone number parser
Another one:
Regular expression explanation: https://regex101.com/r/vU4kS8/3
Code: Select all
F3:: ; F3 hotkey - copies highlighted text to clipboard and replaces/formats phone number
Clipboard := ""
Send, ^c
ClipWait, 1
Clipboard := RegExReplace(Clipboard,
(LTrim Join`r`n
"x) # Options
^(\+?\d[\h(-]{0,3})? # Remove country code, '+', ' ' (space), and '('
(\d{3}) # Area code
[\h)-.]{0,3} # Remove '-', ' ', '.', and ')'
(\d{3}) # Three digits
[\h-.]{0,3} # Remove '-', ' ', and '.'
(\d{4}) # Four digits"
), "$2.$3.$4")
return
Re: Telephone number parser
Thank you guys!
Re: Telephone number parser
I would like to highlight the possibility to match "any non-digit" with \D in the regular expression.
Code: Select all
; This will make a "phone number of the first 10 digits in your clipboard, you can be sloppy and copy any extra non-digits before the phone number, and any extra digits or non digits after it.
num:=RegExReplace(clipboard,"\D+")
msgbox % SubStr(num,1,3) "." SubStr(num,4,3) "." SubStr(num,7,4)
Re: Telephone number parser
Thanks shadowpheonix for sharing. How can we improve the code to strip any leading and trailing space(s)?
Shadowpheonix wrote:AHK can definitely do that...Code: Select all
f12:: CurrentClip := ClipboardAll Clipboard = Send ^c ClipWait Clipboard := RegExReplace(Clipboard, "^.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})$", "$1.$2.$3", 1) Sleep 25 Send ^v Sleep 250 Clipboard := CurrentClip Return
Re: Telephone number parser
Thanks kon. I've updated the code to:
f12::
;CurrentClip := ClipboardAll
Clipboard =
Send ^c
ClipWait
Clipboard := Trim(Clipboard)
Clipboard := RegExReplace(Clipboard, "^.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})$", "$1.$2.$3", 1)
Sleep 25
Send ^v
Sleep 250
Clipboard := CurrentClip
Return
f12::
;CurrentClip := ClipboardAll
Clipboard =
Send ^c
ClipWait
Clipboard := Trim(Clipboard)
Clipboard := RegExReplace(Clipboard, "^.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})$", "$1.$2.$3", 1)
Sleep 25
Send ^v
Sleep 250
Clipboard := CurrentClip
Return
Who is online
Users browsing this forum: iamasink and 352 guests