Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7166549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:25:55+00:00 2026-05-28T14:25:55+00:00

I have a Regex in C# to detect emails in text and then I

  • 0

I have a Regex in C# to detect emails in text and then I put an anchor tag with mailto parameter in it to make it clickable. But if the email is already in an anchor tag, the regex detects the email in the anchor tag and then then next code puts another anchor tag over it. Is there any way in Regex to avoid the emails which are already in the anchor tag?

The regex code in C# is:

string sRegex = @"([\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?)";

Regex Regx = new Regex(sRegex, RegexOptions.IgnoreCase);

and the sample text is:

string sContent = "ttt <a href='mailto:someone@example.com'>someemail@mail.com</a> abc email@email.com";

and the desired output is:

"ttt <a href='mailto:someone@example.com'>someemail@mail.com</a> abc <a href='mailto:email@email.com'>email@email.com</a>";

So, the whole point here is that Regex should only detect those valid emails which are not inside an anchor tag or already clickable and neither should be the anchor tag’s href value inside the anchor tag.

The above given Regex is detecting every possible email in the text which is not desired.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:25:55+00:00Added an answer on May 28, 2026 at 2:25 pm

    Could you use a negative look behind to test for mailto:

    (?<!mailto\:)([\w-]+(.[\w-]+)@([a-z0-9-]+(.[a-z0-9-]+)?.[a-z]{2,6}|(\d{1,3}.){3}\d{1,3})(:\d{4})?)

    Should match anything that is not preceded by mailto:

    I think what is happening is the . in ([\w\-]+(.[\w-])+) is matching too much. Did you mean to use . rather than \. ?

    By escaping the . the following code produces

    someemail@mail.com
    email@email.com
    
    
    public void Test()
    {
    
        Regex pattern = new Regex(@"\b(?<!mailto:)([\w\-]+(\.[\w\-])*@([a-z0-9-]+(.[a-z0-9-]+)?.[a-z]{2,6}|(\d{1,3}.){3}\d{1,3})(:\d{4})?)");
        MatchCollection matchCollection = pattern.Matches("ttt <a href='mailto:someone@example.com'>someemail@mail.com</a> abc email@email.com");
        foreach (Match match in matchCollection)
        {
            Debug.WriteLine(match);
        }
    }
    

    A real world implementation of what it seems like you’re trying to do might look more like this

    Regex pattern = new Regex(@"(?<!mailto\:)\b[\w\-]+@[a-z0-9-]+(\.[a-z0-9\-])*\.[a-z]{2,8}\b(?!\<\/a)");
    MatchCollection matchCollection = pattern.Matches("ttt <a href='mailto:so1meone@example.com'>someemail@mail.com</a> abc email@email.com");
    foreach (Match match in matchCollection)
    {
        Debug.WriteLine(match);
    }
    

    Sorry, you are correct, I hadn’t considered that the negative assertion wouldn’t be greedy enough.

    \b(?!mailto\:)([\w-]+(.[\w-]+)@([a-z0-9-]+(.[a-z0-9-]+)?.[a-z]{2,6}|(\d{1,3}.){3}\d{1,3})(:\d{4})?)

    should work

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following regex to detect text inside <?php (including the tag) '/<\?php(.*)\?>/isU'
I want to detect emails in text format so that I can put an
I have a problem with a difficult regex. I have this expression to detect
I have this regex working but now need to allow numbers without the decimal
I currently have a preg_match to detect http:// and www. etc..... but I want
There have been a few posts about linkifying text using a regex. The most
I have the following regex to detect URLS: /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig However, it doesn't detect urls
I am trying to detect text between 3 or 4 tags and I have
I have this regex /\[\w+:/ Which I use to let me detect when a
Can we have a regex to detect if a number is even ? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.