In my case, content will come it may contain emailaddress in any of the below format.
1. From : [email protected] 2. From:[email protected] 3. rom: sdcasdc <[email protected]> 4. From: "adads" <[email protected]> 5. From: <[email protected]> I need to find the one of the above pattern and extract the email address from this
Regex I tried
From\s*:\s*((["|']?)[\w|\s]+\2)?\s*(\<?)([\w]([\w\-\.\+\'\/]*)@([\w\-\.]*)(\.[a-zA-Z]{2,22}(\.[a-zA-Z]{2}){0,2}))(\>?) All the format matches in my regex. But except 1 and 2, I'll be extract the email address from the regex using group(4).
But in case of 1 and 2, It does not give me full emailaddress.
Please help to alter the regex so that it matches all my case.
(?:…)more often. Use(…)only when needed, e.g. to extract the email itself. It’s getting quite confusing otherwise.From\s*:\s*.*?([^"\n<>]+@[^"\n<>]+\.[^"\n<>]+)appears to work just fine.