Riss
Riss

Reputation: 80

match Email Regular expression from html string

Little trouble by getting the email in html text

     string istr = "<a href = 'mailto:[email protected]'>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 "[email protected]". the output is full html string

any idea?

Upvotes: 2

Views: 139

Answers (3)

Chocoboboy
Chocoboboy

Reputation: 100

you may use match method

emailregex.Match(istr).Groups["mail"].Value

Upvotes: 3

vzades
vzades

Reputation: 112

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

string istr = "<a href = 'mailto:[email protected]'>Email owner</a>";
    string pattern = @"mailto:[A-z  0-9 .].com";
     MatchCollection matchingFinenames = Regex.Matches(istr, pattern);

Upvotes: 1

Casperah
Casperah

Reputation: 4554

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";

Upvotes: 1

Related Questions