Home > other >  Regular expression detects phone numbers correctly but not when phone numbers are inline
Regular expression detects phone numbers correctly but not when phone numbers are inline

Time:10-04

I'm trying to remove Dutch phone numbers from a string via a regular expression. The phone numbers to remove can contain spaces, and dashes and the country code.

These are typically used formats to be detected:

06-12345678
0612345678
0612345678
0101234567
010-1234567
010 1234567
010 12 34 567
010 12 34567
010 1234 567
 31612345678
0031123456789

So far I have: ^(?:0|(?:\ |00) ?31 ?)(?:(?:[1-9] ?(?:[0-9] ?){8})|(?:6 ?-? ?[1-9] ?(?:[0-9] ?){7})|(?:[1,2,3,4,5,7,8,9]\d ?-? ?[1-9] ?(?:[0-9] ?){6})|(?:[1,2,3,4,5,7,8,9]\d{2} ?-? ?[1-9] ?(?:[0-9] ?){5}))$

This works perfect when phone numbers are on a separate line but NOT when they're inline like so:

this is a list of numbers inline 06-12345678 gggf0612345678 06 12345678 0101234567 010-1234567 dfgfdg010 1234567

Test it here: https://regex101.com/r/jOEFwS/2

I've checked here, but that is for US phone numbers:

How can I change my expression to capture inline?

CodePudding user response:

You need to remove ^ from the start and $ at the end.

Some more info: In regex101 you have the m flag, which makes ^ and $ line based, without it ^ is start of string and $ is end of string.

  • Related