user3283015
user3283015

Reputation: 489

How can I remove everything in a line except matching regex pattern?

I use ^.*?(\d{3}\D?\d{3}\D?\d{4}).*$ and replace with \1 or $1 so that everything in each separate line is removed except for the telephone number. Example link https://regex101.com/r/jK6eD8/3.

Basically it works like below:

Line 1: this is crap text, only 818-333-2323 is kept in line

line 2: only the following number 4445553333 is kept in line.

What I need help with is finding matching regex patterns for the phone formats below, and remove everything else in its respective line EXCEPT the matching phone number JUST LIKE THE ABOVE LINK. The formats are below.

07123452670
07812 345 931
07412 123466
00447912345188
+971557017442
+971 557 856 832
0414 934 993

So basically, I need a regex for matching 11 digits. (07123456270)

Matching 5 digits, followed by space, followed by 3 digits, followed by space, followed by 3 digits. (07812 345 931)

Matching 5 digits, followed by space, followed by 6 digits (07412 123466)

Matching 14 digits (12345678901234)

Matching a + sign followed after with 12 digits (+971557017442)

Matching + followed with 3 digits, space, followed by 3 digits, space, 3 more digits (+971 557 856 832)

Last one, 4 digits, space, 3 digits, space, 3 digits. (0414 934 993)

Upvotes: 0

Views: 68

Answers (2)

SamWhan
SamWhan

Reputation: 8332

If the text you're analyzing doesn't contain other "long" numbers, you could just get strings of digits with optional spaces, full stops and dashes between them. It could look like this:

^.*?(\d[\d .-]{9,13}\d).*$

The match group must consist of

  1. a digit, followed by
  2. 9-13 characters either being a digit, a space, a dash or a full stop. Then followed by
  3. a final digit.

This isn't that strict on the composition of the number though, so it might not suit your needs. But then again, it might ;)

Regards

Upvotes: 0

nessuno
nessuno

Reputation: 27042

This regex meets the requirements: ^.*?(\+?(?:\d{11,14})|(?:\d{5}\s(?:\d{3}\s\d{3}|\d{6}))|(?:\d{3}(?:\s\d{3}){3})|(?:\d{4}\s\d{3}\s\d{3})).*$

As you can see here: https://regex101.com/r/lY3jW0/1

I hope it helps

Upvotes: 1

Related Questions