-4

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
Community
  • 1
  • 1
Manish Sharma
  • 2,406
  • 2
  • 16
  • 31

3 Answers3

2

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);
Community
  • 1
  • 1
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
1

A simple Regex to strip anything in a mailto tag would be

<mailto:(.*?)>
Droxx
  • 197
  • 1
  • 8
0

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!

Srb1313711
  • 2,017
  • 5
  • 24
  • 35