Regex Find email address between First Two Instances of Strings

joepitz1

I have raw text from a chain of emails.

For all inquiries please reach out
From: [email protected] At: 01/27/21 23:29:28To: CompanyA
Cc: [email protected], [email protected] Subject: this is the subject line
From: CompanyB(company) <[email protected]>
Sent: Wednesday, January 27, 2021 12:51 PM
From: [email protected] At: 01/27/21 23:29:28To: CompanyA
Cc: [email protected], [email protected] Subject: tect

Through Regex I need to capture the email addresses between the first word From to the first Subject. In the above the match should be:
[email protected]
[email protected]
[email protected]

I do have (\n){0,1}([\w.]@[\w+-.]) to get email addresses. I will match through Python Regex Lib.

The fourth bird

One option is to use 2 patterns with re.

First find all the matches from From: till the first occurrence of Subject:

(?s)\bFrom:.*?\bSubject:

Then for all those matches, get the email address like patterns without matching < and >

[^<>\s@]+@[^@\s<>]+

Example

import re
s = ("For all inquiries please reach out\n"
            "From: [email protected] At: 01/27/21 23:29:28To: CompanyA\n"
            "Cc: [email protected], [email protected] Subject: this is the subject line\n"
            "From: CompanyB(company) <[email protected]>\n"
            "Sent: Wednesday, January 27, 2021 12:51 PM\n"
            "From: [email protected] At: 01/27/21 23:29:28To: CompanyA\n"
            "Cc: [email protected], [email protected] Subject: tect")

for match in re.findall(r"(?s)\bFrom:.*?\bSubject:", s):
        print(re.findall(r"[^<>\s@]+@[^@\s<>]+", match))

Output

['[email protected]', '[email protected],', '[email protected]']
['[email protected]', '[email protected]', '[email protected],', '[email protected]']

If you don't want to cross another occurrence of From: or Subject, you can use a negative lookahead to check if the line does not contain any of the strings.

^From:.*(?:\r?\n(?!From|.*\bSubject:).*)*\r?\n.*\bSubject:

Regex demo

Example

for match in re.findall(r"(?m)^From:.*(?:\r?\n(?!From|.*\bSubject:).*)*\r?\n.*\bSubject:", s):
        print(re.findall(r"[^<>\s@]+@[^@\s<>]+", match))

Output

['[email protected]', '[email protected],', '[email protected]']
['[email protected]', '[email protected],', '[email protected]']

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Regex: Match all characters between two strings

分類Dev

python regex for extracting strings between certain two strings

分類Dev

regex command line linux - select all lines between two strings

分類Dev

Find user from email address

分類Dev

What is the difference between these two instances?

分類Dev

How to split first two characters and followed by next two characters after the period of a email address ([email protected]=tema)

分類Dev

I need Regex to mask email address in javascript

分類Dev

How to find number of observation between first observation and first two consecutive negative observations in r

分類Dev

Regex discard catch between strings

分類Dev

Can two identical strings be two separate instances in C#?

分類Dev

Common strings between two sequences

分類Dev

Notepad++ Find strings 50 characters or longer between two vertical bars

分類Dev

Capture email address within non-capturing group RegEx

分類Dev

RegEx Find Text Between Dashes

分類Dev

Find the first letter and sign of a sentence with Regex

分類Dev

Getting the common character count between two strings

分類Dev

Extracting a string between other two strings in R

分類Dev

Extracting a string between other two strings in R

分類Dev

Extracting a string between other two strings in R

分類Dev

Extracting a string between other two strings in R

分類Dev

Python3 toggle between two strings

分類Dev

Replace all occurrences of a character between two strings

分類Dev

How to get everything in between these two strings?

分類Dev

How to find two strings that are close to each other

分類Dev

Is it possible to push/pull variables between two instances of R?

分類Dev

Android: Is there a way to program "collision detection" between two instances of an object?

分類Dev

SQL - find all instances where two columns are the same

分類Dev

How to ignore strings where a specific pattern occurs between two strings?

分類Dev

iOS How to find multiple strings between 2 strings in a huge string?

Related 関連記事

  1. 1

    Regex: Match all characters between two strings

  2. 2

    python regex for extracting strings between certain two strings

  3. 3

    regex command line linux - select all lines between two strings

  4. 4

    Find user from email address

  5. 5

    What is the difference between these two instances?

  6. 6

    How to split first two characters and followed by next two characters after the period of a email address ([email protected]=tema)

  7. 7

    I need Regex to mask email address in javascript

  8. 8

    How to find number of observation between first observation and first two consecutive negative observations in r

  9. 9

    Regex discard catch between strings

  10. 10

    Can two identical strings be two separate instances in C#?

  11. 11

    Common strings between two sequences

  12. 12

    Notepad++ Find strings 50 characters or longer between two vertical bars

  13. 13

    Capture email address within non-capturing group RegEx

  14. 14

    RegEx Find Text Between Dashes

  15. 15

    Find the first letter and sign of a sentence with Regex

  16. 16

    Getting the common character count between two strings

  17. 17

    Extracting a string between other two strings in R

  18. 18

    Extracting a string between other two strings in R

  19. 19

    Extracting a string between other two strings in R

  20. 20

    Extracting a string between other two strings in R

  21. 21

    Python3 toggle between two strings

  22. 22

    Replace all occurrences of a character between two strings

  23. 23

    How to get everything in between these two strings?

  24. 24

    How to find two strings that are close to each other

  25. 25

    Is it possible to push/pull variables between two instances of R?

  26. 26

    Android: Is there a way to program "collision detection" between two instances of an object?

  27. 27

    SQL - find all instances where two columns are the same

  28. 28

    How to ignore strings where a specific pattern occurs between two strings?

  29. 29

    iOS How to find multiple strings between 2 strings in a huge string?

ホットタグ

アーカイブ