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 6983933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:29:02+00:00 2026-05-27T18:29:02+00:00

In my MatchCollection, I get matches of the same thing. Like this: string text

  • 0

In my MatchCollection, I get matches of the same thing. Like this:

string text = @"match match match";
Regex R = new Regex("match");
MatchCollection M = R.Matches(text);

How does one remove duplicate matches and is it the fastest way possible?

Assume “duplicate” here means that the match contains the exact same string.

  • 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-27T18:29:02+00:00Added an answer on May 27, 2026 at 6:29 pm

    Linq

    If you are using .Net 3.5 or greater such as 4.7, linq can be used to remove the duplicates of the match.

    string data = "abc match match abc";
    
    Console.WriteLine(string.Join(", ", 
    
    Regex.Matches(data, @"([^\s]+)")
         .OfType<Match>()
         .Select (m => m.Groups[0].Value)
         .Distinct()
    
    ));
    
    // Outputs abc, match
    

    .Net 2 or No Linq

    Place it into a hastable then extract the strings:

    string data = "abc match match abc";
    
    MatchCollection mc = Regex.Matches(data, @"[^\s]+");
    
    Hashtable hash = new Hashtable();
    
    foreach (Match mt in mc)
    {
        string foundMatch = mt.ToString();
        if (hash.Contains(foundMatch) == false)
            hash.Add(foundMatch, string.Empty);
    
    }
    
    // Outputs abc and match.
    foreach (DictionaryEntry element in hash)
        Console.WriteLine (element.Key);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I use this string showPattern = @return new_lightox\(this\);>[a-zA-Z0-9(\s),!\?\-:'&%]+</a>; MatchCollection showMatches = Regex.Matches(pageSource, showPattern);
I have an input string: access_token=f34b46f0f109d423sd4236af12d1bce7f10df108ec2183046b8f94641ebe&amp;expires_in=0&amp;user_id=37917395 Source code: Regex regex = new Regex(access_token=([^&]+)); MatchCollection
I wonder if it is possible to get MatchCollection with all matches even if
Possible Duplicate: regex for URL including query string I have a text or message.
I have the following very simple regex, which matches HTML tags in a string.
all! I have a text like this Some text with uri http://test.com and other
i'm using regex to get some information from a website, i have this code:
Let's say I'm matching with a pattern that has subexpressions like this: Regex myRegex
Is this good or bad: foreach (Match match in serverNameRegex.Matches(loginPage)) { .... } or
Let's say I have a multi-line string like this: STARTFRUIT banana ENDFRUIT STARTFRUIT avocado

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.