I need to parse the email address from a mailto tag. I am looking for a way to do this via RegEx in C#.
Example input:
<mailto:abc@xyz.com>
Example output:
abc@xyz.com
I need to parse the email address from a mailto tag. I am looking for a way to do this via RegEx in C#.
<mailto:abc@xyz.com>
abc@xyz.com
In general, it's a very bad idea to use regular expressions for HTML parsing. Instead, take a look at the Html Agility Pack. For the specific input you provided, you may use:
(?<=\<mailto:).*(?=\>)
Here's a code sample:
var emailTag = "<mailto:abc@xyz.com>";
var emailValue = Regex.Match(emailTag, @"(?<=\<mailto:).*(?=\>)").Value;
Console.WriteLine(emailValue);
You could use:
[\w\d]+\@[\w\d]+\.com
[\w\d] <----This matches any letter or character. \w matches any letter. \d matches anynumber.
+ <----One or more of previous item, in this case [\w\d]+ one or more letters or numbers
\@ <----Simply matches the @ symbol but it needs to be escaped with a \ as it is a special character
[\w\d]+ <----Same again
\. <---- Same concept as the @ as . is a special character so it needs to be escaped
In your example:
[\w\d]+=abc
\@=@
[\w\d]+=xyz
\.=.
com=com
If your wanting to match special characters as well as letters and digits then just replace [\w\d]+ with [\S]+ (make sure s is capital).
[\S]+ <---Matches anything that is not a space.
You will have to do variations to include .co.uk and .org etc.
http://www.regular-expressions.info/reference.html <----This is very useful!