Search code examples
phpregextext-extractiontext-parsing

Get telephone number from a string containing labelled contact details


I am having some trouble with my preg_match() code.

$text = "tel: 012 213 123. mobil: 0303 11234 \n address: street 14";
$regex_string = '/(tel|Tel|TEL)[\s|:]+(.+)[\.|\n]/';

preg_match($regex_string , $text, $match);

And I get this result in $match[2]

"012 213 123. mobil: 023 123 123"

First question:

I want the regex to stop at the . (dot) but it doesn't. Can someone explain to why it isnt?

Second question:

preg_match uses () to get their match.

Is it possible to skip the parentheses surrounding the different "Tel" and still get the same functionality?


Solution

  • This should do:

    /tel(?:\s|:)+([^.]+)(?:\.|$)/i
    

    + is a greedy quantifier, which means it'll match as many characters as possible.

    To your second question: in this particular case you just need to use case-insensitive match (i flag). Generally, you could use (?:...) syntax, example of which you could see in the end match. Square brackets are used for character classes.