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

  • Home
  • SEARCH
  • 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 275309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:44:42+00:00 2026-05-12T00:44:42+00:00

There is a method Regex.Replace(string source, string pattern, string replacement) The last parameter supports

  • 0

There is a method

Regex.Replace(string source, string pattern, string replacement)

The last parameter supports pattern replacements such as ${groupName} and others (but I don’t know the group name in the run-time).

In my case I have the dynamically created pattern like:

(?<c0>word1)|(?<c1>word2)|(?<c2>word3)

My purpose is to replace each group with the value depending on group name. For example, the word “word1” will be replaced with <span class="c0">word1</span>. This is for search results highlighting like google.

Is it possible to do this using the method above not using overloaded method with MatchEvaluator parameter?

Thanks in advance!

  • 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-12T00:44:43+00:00Added an answer on May 12, 2026 at 12:44 am

    I don’t think using the ${groupname} in the manner you suggested is feasible unless I am misunderstanding the exact replacement that’s being performed. The reason is the replacement string has to be constructed in such a way that it accounts for each group name. Since they are dynamically generated this doesn’t make it possible. In other words how, in 1 statement, do you design a replacement string that would cover c0…cn and substitute their respective capture values? You could loop through the names but how would you keep the modified text intact to perform 1 replacement per groupname?

    I do have a possible solution for you though. It still uses the MatchEvaluator overload, but with some lambda expressions and LINQ you can get this down to 1 line. However, I’ll format it for clarity below. Perhaps this will fit your needs or point you in the right direction.

    string text = @"The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.";
    string[] searchKeywords = { "quick", "fox", "lazy" };
    
    // build pattern based on keywords - you probably had a routine in place for this
    var patternQuery = searchKeywords
                            .Select((s, i) => 
                                String.Format("(?<c{0}>{1})", i, s) +
                                (i < searchKeywords.Length - 1 ? "|" : ""))
                            .Distinct();
    string pattern = String.Join("", patternQuery.ToArray());
    Console.WriteLine("Dynamic pattern: {0}\n", pattern);
    
    // use RegexOptions.IgnoreCase for case-insensitve search
    Regex rx = new Regex(pattern);
    
    // Notes:
    // - Skip(1): used to ignore first groupname of 0 (entire match)
    // - The idea is to use the groupname and its corresponding match. The Where
    //   clause matches the pair up correctly based on the current match value
    //   and returns the appropriate groupname
    string result = rx.Replace(text, m => String.Format(@"<span class=""{0}"">{1}</span>", 
                        rx.GetGroupNames()
                        .Skip(1)
                        .Where(g => m.Value == m.Groups[rx.GroupNumberFromName(g)].Value)
                        .Single(),
                        m.Value));
    
    Console.WriteLine("Original Text: {0}\n", text);
    Console.WriteLine("Result: {0}", result);
    

    Output:

    Dynamic pattern: (?<c0>quick)|(?<c1>fox)|(?<c2>lazy)
    
    Original Text: The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
    
    Result: The <span class="c0">quick</span> brown <span class="c1">fox</span> jumps over the <span class="c2">lazy</span> dog. The <span class="c0">quick</span> brown <span class="c1">fox</span> jumps over the <span class="c2">lazy</span> dog.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 208k
  • Answers 208k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I didn't find the origins of the problem. However problem… May 12, 2026 at 9:35 pm
  • Editorial Team
    Editorial Team added an answer The public API isn't available yet unfortunately... I am eagerly… May 12, 2026 at 9:35 pm
  • Editorial Team
    Editorial Team added an answer Adobe Air provides a file I/O API via the FileStream… May 12, 2026 at 9:35 pm

Related Questions

I'm using LINQ to SQL to pull records from a database, sort them by
I was wondering if anyone had put together something or had seen something equivalent
I have a web service method that takes a network login (string) as a
I have a list that needs ordering say: R1-1 R1-11 R2-2 R1-2 this needs

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.