Extracting phone numbers from a free form text in python by using regex

前端 未结 4 611
遇见更好的自我
遇见更好的自我 2020-12-22 11:49

I have to extract phone numbers from free form of texts.

How can I manage it by using reg-ex in python?

I have found for one in order to extract e-mail addre

4条回答
  •  遥遥无期
    2020-12-22 12:26

    You have to build a pattern to be able to match it with regexp. The question is what is the format you are looking for?

    To be able to do this you should do some research on the use-cases how the phone numbers show up.

    So I'd expect you to define what are you meaning by matching phone numbers.

    • Is it a specific format that you looking for, always consistent through the free text?
    • Or can you define the string with a pattern that matches a phone number, by the country code (+xx) and then an specific number of digits?

    I just mean that there is a huge difference between: - I want to match phone numbers from a text that can be from any country, mobile or landline, in any format, with random spaces and (,) chars in it or - I want to match phone numbers from Hungary, with a +xx(space)xxxxxxx(space) format, that is always consistent.

    Summary: To be able to build a pattern with regexp and use it to match all the phone numbers in your text, you have to be aware of the different representations, meaning what are you expecting a phone number will look like. If your pattern is not correct, you might miss a lot of phone numbers.

    Hope this code serves a good cause, V

提交回复
热议问题