HerrimanCoder
HerrimanCoder

Reputation: 7218

PHP regex removes segment before and including period

I have a chunk of PHP code that parses email addresses out of a large string that contains a jumble of email addresses:

$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';
preg_match_all($pattern, $emailBatch, $matches);

This works perfectly except that any email address that contains a period before the @ gets truncated.

Example: [email protected] becomes [email protected]

How can I modify the regex to allow/accept periods (and any other legal character for email addresses) in the address?

Upvotes: 0

Views: 34

Answers (1)

Jakub Krawczyk
Jakub Krawczyk

Reputation: 970

In the first group of the pattern you need to add a dot:

$pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';
preg_match_all($pattern, $emailBatch, $matches);

Because it is not specified in the first group it catches only the second part (that is right before @ sign).

Upvotes: 2

Related Questions