[Python] Python - regex to grab specific lines from text

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

  1. Stack

    Stack Membro Participativo

    I need to grab specific details being parsed in from email bodies, in this case the emails are plain text and formatted like so:

    imbad@regex.com
    John Doe
    +16073948374
    2021-04-27T15:38:11+0000
    14904


    The above is an example output of print(body) parsed in from an email like so:

    def parseEmail(popServer, msgNum):
    raw_message=popServer.retr(msgNum)[1]
    str_message=email.message_from_bytes(b'\n'.join(raw_message))
    body=str(str_message.get_payload())


    So, if I needed to simply grab the email address and phone number from body object, how might I do that using regex?

    I understand regex is most certainly overkill for this, however I'm only repurposing an existing in-house utility that's already written to utilize regex for more complex queries, so it seems the simplest solution here would to modify the regex to grab the desired text. attempts to utilize str.partition() resulted in other unrelated errors.

    Thank you in advance.

    Continue reading...

Compartilhe esta Página