Convert a list of phone numbers to E.164

688 views
Skip to first unread message

Joe Armendariz

unread,
Aug 3, 2022, 6:17:03 PM8/3/22
to TextSoap
I have a list of phone numbers in various formats such as these. I'

I'd like to make all numbers uniform with the E.164 phone number format: The cleaner would do these things:
- Add +1 at the beginning but only if the phone number doesn't already have +1
- Strips any characters such as (,),or-
- Strips any spaces and tabs
- If the phone number begins with 1, convert it to +1

The issue I'm having is:
-  Phones numbers in the `(818) 500-1234` format, the cleaned result still has an extra space after the area code and looks like this +1818 5001234

 - Phone numbers that have a leading 1 get +1 attached in front so it turns out like such.. +118185001234

Can you please take a look at my cleaner and provide any suggestions/improvements? I'd appreciate it.  Thank you.
 Screen Shot 2022-08-03 at 5.47.55 PM.png

Mark Munz

unread,
Aug 4, 2022, 2:17:52 PM8/4/22
to TextSoap
CleanShot 2022-08-04 at 11.09.25.jpg
Here is my solution:

Instead of for each line, I use If Matches Regex and look for a phone number lines. This allows for the numbers to be mixed with actual text (although it makes the assumption that each phone number is on its own line). If the text is purely phone numbers, then For Each Line will suffice.

Next, there are four Regex Find and Replace actions to clean up the formatting.
First, I look for (,),- and whitespace (tabs + spaces) and replace them with nothing (effectively deleting them)
Second, I strip off any 1 or +1 prefix to standardize all the phone numbers (you'll need to make some tweaks if this list includes international numbers)
Third, once I've stripped off any stray 1 or +1 prefix, I can then put that prefix in all the lines. This approach is simpler than trying to keep existing +1, 1 prefix while changing the others.
Finally, I add a space after the 4th digit. The replace is `$0 ` (with a space after $0).

That should give you the phone numbers in your preferred format.


--
You received this message because you are subscribed to the Google Groups "TextSoap" group.
To unsubscribe from this group and stop receiving emails from it, send an email to textsoap+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/textsoap/ba5d9e41-7c29-4748-8b33-4c7f1db4290dn%40googlegroups.com.


--
Mark Munz
unmarked software
http://www.unmarked.com/

Joe

unread,
Aug 5, 2022, 4:58:34 PM8/5/22
to TextSoap
Thank you for the solution it works great. I'll continue to support TextSoap.
Reply all
Reply to author
Forward
0 new messages