2

Little trouble by getting the email in html text

     string istr = "<a href = 'mailto:myemail@mail.com'>Email owner</a>";
        protected void Button1_Click(object sender, EventArgs e)
        {

        // var emailregex = new Regex(@"\b\S+@mail\.com\b", RegexOptions.IgnoreCase | RegexOptions.Compiled);

       var emailregex = new Regex(@"\b(?<mail>[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)\b", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            Label1.Text = emailregex.Replace(istr, "${mail}");
        }

I failed to get the "myemail@mail.com". the output is full html string

any idea?

Riss
  • 80
  • 6
  • 2
    [Not a good idea](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) parsing HTML text with regex. – Steve Dec 26 '13 at 09:53

3 Answers3

3

you may use match method

emailregex.Match(istr).Groups["mail"].Value
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Chocoboboy
  • 100
  • 8
1

If you want the email you should do something like this:

Match m = emailregex.Match(istr);
if (m.Success)
    Label1.Text = m.Groups["mail"].Value;
else
    Label1.Text = "No match";
Casperah
  • 4,504
  • 1
  • 19
  • 13
1

This will give you the email address with mailto you can substring it.

string istr = "<a href = 'mailto:myemail@mail.com'>Email owner</a>";
    string pattern = @"mailto:[A-z  0-9 .].com";
     MatchCollection matchingFinenames = Regex.Matches(istr, pattern);
vzades
  • 112
  • 2
  • 4