[Python] How to optimize regex search across multiple patterns in Python?

Discussão em 'Python' iniciado por Stack, Janeiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Python script that uses multiple regular expressions to search through a file's content. Here's the relevant code snippet:

    PATTERNS = {
    "email": re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b"),
    "phone": re.compile(r"\b\d{3}-\d{3}-\d{4}\b"),
    "date": re.compile(r"\b\d{4}-\d{2}-\d{2}\b")
    }

    def readfilecontent(filepath):
    with open(filepath,'r', encoding='utf-8', errors='ignore') as file:
    return file.read()

    filecontent = readfilecontent("path/ToFile")
    for key, pattern in PATTERNS.items():
    matches = pattern.findall(filecontent)
    if matches:
    for match in matches:
    print(match)


    PATTERNS is a dictionary where the key is the pattern's name, and the value is a precompiled regex object (created using re.compile).

    filecontent contains the file's content as a single string. The script works correctly but takes several minutes to process each file due to the large size of the files and the number of patterns.

    Is there a way to speed this up, either by restructuring the code or using a more efficient library or approach? For example, would combining patterns, parallel processing, or other methods improve performance?

    Continue reading...

Compartilhe esta Página