Telephone number parser

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DevinC
Posts: 10
Joined: 01 Sep 2016, 15:11

Telephone number parser

01 Sep 2016, 16:11

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
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Telephone number parser

01 Sep 2016, 16:45

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
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Telephone number parser

01 Sep 2016, 17:02

Another one:

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
Regular expression explanation: https://regex101.com/r/vU4kS8/3
DevinC
Posts: 10
Joined: 01 Sep 2016, 15:11

Re: Telephone number parser

05 Sep 2016, 14:25

Thank you guys!
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Telephone number parser

05 Sep 2016, 15:10

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)
DevinC
Posts: 10
Joined: 01 Sep 2016, 15:11

Re: Telephone number parser

14 Mar 2017, 15:03

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
DevinC
Posts: 10
Joined: 01 Sep 2016, 15:11

Re: Telephone number parser

14 Mar 2017, 15:32

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], ReyAHK and 266 guests