sanchez
sanchez

Reputation: 4530

Extract names and email addresses form a text

I am trying to extract names and email addresses from a list of emails in this format:

...
Sent: 19 July 2019 14:25
To: John Dough <[email protected]>
...

This extracts email addresses perfectly:

/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i

How can I also get the string between To: and < example: John Dough ?

Upvotes: 1

Views: 397

Answers (1)

The fourth bird
The fourth bird

Reputation: 163342

Yo might use 2 capturing groups (assuming the name part can not contain characters < and > If those chars can occur, you could use a non greedy quantifier (.*?) instead of ([^<>]+)

\bTo: ([^<>\r\n]+) <([a-z0-9_+.-]+@[a-z0-9-]+\.[a-z]{2,4}(?:\.[a-z]{2})?)>

Regex demo

About ([^<>\r\n]+)

  • ( Capture group
    • [^<>\r\n]+ Negated character class, match 1+ times any char except < > or a newline
  • ) Close group

Or make the match a bit broader matching a non whitespace char except an @

\bTo: ([^<>\r\n]+) <([^\s@]+@[^\s@]+)>

Regex demo

Upvotes: 3

Related Questions