Reputation: 4530
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
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})?)>
About ([^<>\r\n]+)
(
Capture group
[^<>\r\n]+
Negated character class, match 1+ times any char except <
>
or a newline)
Close groupOr make the match a bit broader matching a non whitespace char except an @
\bTo: ([^<>\r\n]+) <([^\s@]+@[^\s@]+)>
Upvotes: 3