[Python] Python Regex (Pattern Matching) phone number start with area code search

Discussão em 'Python' iniciado por Stack, Julho 16, 2025.

  1. Stack

    Stack Membro Participativo

    I tried to write logic to extract the phone number which starts with certain area codes (first 3 digits of the phone number) in a given data file. My code is:

    import re

    data = "This is the sample test data. 2247279133 224dfa7279133 dhana 5107279133 subha 123456789 "

    pattern = re.compile(r"((224|510)\d{7})")
    matches = pattern.findall(data)

    for match in matches:
    print (match[0])


    I am getting the expected output as below:


    2247279133

    5107279133
    ​

    Though I am getting the expected output I would like to know below things:

    1. Is this an efficient way?
    2. Is it possible to pass the list of area codes as an array variable instead of hardcoding (224|510)?
    3. what is the recommended way to search these kinds of phone numbers over large file of 10 GB?

    Continue reading...

Compartilhe esta Página