fis
fis

Reputation: 21

Regex - Extract phone numbers from string

I need to extract two phone numbers from a string with delimiters (tilde). The tricky part is the format of the phone numbers can vary.

The string pattern stays the same. But the formatting of the phone numbers can be one of three types

(1)  4 digit extensions. (ex. 1001)
(2) 10 digit (5551112222)
(3) 10 digit with country code (+15558889999)

I need the extension or 10 digit number with the +1 country code stripped off. So

(1) 1001 = 1001
(2) 5551112222 = 5551112222
(3) +15558889999 = 5558889999

Example String

2019/02/06/2019-02-06T084903~call~5551112222~+15558889999~231a6a62-c1c8-43a8-ac2e-f8428237385c.WAV

From the string above, I need to extract two phone numbers with correct 10 digit formatting

(1) 5551112222
(2) 5558889999

So far, I have the following regex expression:

(?<=\~)(.*?)(?=\~)

This gives me three groups

(1) Call
(2) 5551112222
(3) +15558889999

But, what I need is two groups with correct formatting

(1) 5551112222
(2) 5558889999

I'm using this regex pattern with Integromat so no coding language solutions will work in this case. It must be 100% regex.

Appreciate any help on this. Thanks!

Upvotes: 1

Views: 398

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

You may use

(?<=~\+|~)([0-9]+)(?=~)

See the regex demo

If there is a problem with lookbehind, use a bit modified variant:

(?:(?<=~\+)|(?<=~))([0-9]+)(?=~)

Details

  • (?<=~\+|~) - there must be ~+ or ~ immediately to the left of the current location
  • ([0-9]+) - Group 1: one or more digits
  • (?=~) - there must be ~ immediately to the right of the current location.

Upvotes: 1

Related Questions