pythonregexregex-group

Regex for phone numbers


I want to catch the phone number from a text using regex.

Examples:

I have this regex which finds the phone number very well: ^((\(?\+45\)?)?)(\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2})$

and it catches all the numbers below well.

But I cannot catch the "tel.", "tlf", "mobil:", etc that could be before the number. And also, if another letter comes after the last digit, it doesn't take number anymore, but it should.

These examples are not covered:

tel.: +45 09827374, +45 89895867, some kind of text... 
mobil: +45 20802020, +45 20802001,
tlf.: +45 5555 1212 
tlf: +4567890202Girrafe

If helpful, I found this regex: '\btlf\b\D*([\d\s]+\d)' which can extract the number and the tlf and also stop before it finds a new character which is represented by a letter.

So I tried to combine them and I obtained this but it doesn't work: \b(tlf|mobil|telephone|mobile|tel)\b\D*(^((\(?\+45\)?)?)(\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2})$)

Expected output:

Can you help me please?


Solution

  • If you want the full match only:

    (?:\b(?:tlf|mobile?|tel(?:ephone)?)[.:\s]+)?(?:\(\+45\)|\+45)?\s*\d{2}(?:\s?\d{2}){3}(?!\d)
    

    The pattern matches:

    See a regex demo