Cleaning a phone number array with REGEX Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Gate
Posts: 27
Joined: 31 Jan 2021, 20:00

Cleaning a phone number array with REGEX

28 Jul 2021, 16:06

I have a list of poorly formatted phone numbers in an array. How would I format them all using one line of REGEX?

Code: Select all

a := ["+11231231234","+67 123 123 1234","+1 123 123 1234","123 123 1234"," 123 123 1234","(123) 123 1234","123-123-1234","1231231234","12 31 23 12 34","(123) 123-1234","+1 (123) 123-1234","+67 (123) 123-1234"],

for k, v in a.Clone()
	a[k] := RegExMatch(v, "((\d\D*){10})$")   ; does not filter out \D like I intended
	
; GOAL: a[k] = "1231231234"
	
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Cleaning a phone number array with REGEX  Topic is solved

28 Jul 2021, 17:20

Code: Select all

a := ["+11231231234","+67 123 123 1234","+1 123 123 1234","123 123 1234"," 123 123 1234","(123) 123 1234","123-123-1234","1231231234","12 31 23 12 34","(123) 123-1234","+1 (123) 123-1234","+67 (123) 123-1234"]

for k, v in a
   MsgBox, % RegExReplace(v, ".(?=(\D*\d\D*){10})|\D")
User avatar
mikeyww
Posts: 26875
Joined: 09 Sep 2014, 18:38

Re: Cleaning a phone number array with REGEX

28 Jul 2021, 17:29

Code: Select all

tel := ["+11231231234"     , "+67 123 123 1234"  , "+1 123 123 1234", "123 123 1234"  , " 123 123 1234"
       ,"(123) 123 1234"   , "123-123-1234"      , "1231231234"     , "12 31 23 12 34", "(123) 123-1234"
       ,"+1 (123) 123-1234", "+67 (123) 123-1234"]
new  =
For each, ph in tel
 new .= "`n" SubStr(RegExReplace(ph, "[+\- ()]"), -9)
MsgBox, 64, Result, % SubStr(new, 2)
Gate
Posts: 27
Joined: 31 Jan 2021, 20:00

Re: Cleaning a phone number array with REGEX

28 Jul 2021, 18:13

Thank you both for your answers. I really needed a short, one line answer for a reason that's hard to explain, so teadrinker really took the cake here. But definitely both solutions work, I tested them both out! So thanks both of you for your time!
User avatar
mikeyww
Posts: 26875
Joined: 09 Sep 2014, 18:38

Re: Cleaning a phone number array with REGEX

28 Jul 2021, 19:14

I agree that mine is way too long! Best wishes.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5, justcop, Rohwedder and 175 guests