tags:

views:

27

answers:

2

Hai.. kindly check my file content

From: Bablu Pal <[email protected]>
To: Anirban Ghosh <[email protected]>; Arindam <[email protected]>; arpita <[email protected]>; Arpita Das <[email protected]>; Bijoy Mandal <[email protected]>; Goutam <[email protected]>; "[email protected]" <[email protected]>; "[email protected]" <[email protected]>; Kris <[email protected]>; "[email protected]" <[email protected]>; "[email protected]" <[email protected]>; Poonam <[email protected]>; Priyam Paul <[email protected]>; Sourav Sarkar <[email protected]>; Souvik <[email protected]>; "[email protected]" <[email protected]>; "[email protected]" <[email protected]>
Sent: Mon, 30 August, 2010 8:40:18 PM Regards    sangeeta
    ---------- Forwarded message ----------

from this file content, I want to retrieve only the valid email ids. how I will write the regular expression in PHP

A: 

Please see this question

rerun
A: 

Validating e-mail addresses with regular expressions is hard (and, for practical purposes, more or less impossible, since even a valid address might not lead to an actual mailbox). A better tool for this is a parser.

Finding e-mail addresses in a chunk of text, however, can be done with some accuracy. But you are likely to find some false positives (some things may look like an e-mail address but aren't), and/or miss some valid but "strange" addresses (like Tim\ O'[email protected]). Therefore, you should subject the strings you found as e-mail addresses to a parser and have it check if it's actually a valid address. Or, even better, try to send mail to it and see if you get a response.

That said, a good bet (and one that works on your examples) is

\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\b

(courtesy of RegexBuddy)

In PHP:

preg_match_all('/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\b/i', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];

Note the /i modifier to make it case-insensitive.

Tim Pietzcker
Thanks a lot.. great
JAMES