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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:43:16+00:00 2026-06-11T21:43:16+00:00

Suppose I have a regex pattern and I want to replace the matches of

  • 0

Suppose I have a regex pattern and I want to replace the matches of the pattern with something else. In the current pattern there are two groups that will match and each one is numbered ($1 and $2):

Regex pattern = new Regex(@"\[([a-zA-Z0-9_\-]+)\^=([^\]]+)\]");
string replacement = "[starts-with(@$1,$2)]";

Example CSS Selector:

[id^="blah"]

Expected output:

[start-swith(@ID,"blah")] // Note ID is capitalized

Here is another regex pattern:

Regex pattern = new Regex(@"\[([a-zA-Z0-9_\-]+)\*=([^\]]+)\]");
string replacement = "[contains(@$1,$2)]");

When I’m performing the replace, is there any way to capitalize the matches in group $1?

Note: I have numerous patterns that get added to a list and they’re paired with their replacement string, so I have to make the solution works for all of the replacements which require capitalization of some of the matching groups.

Update

I think I just thought of a possible solution: convert the replacement string to a MatchEvaluator and return the capitalized group matches when needed. I think this might work:

Regex pattern = new Regex(@"\[([a-zA-Z0-9_\-]+)\^=([^\]]+)\]");
MatchEvaluator evaluator = new MatchEvaluator((Match m) =>
    {
        return string.Format("[starts-with(@{0},{1})]", m.Groups[1].Value.ToUpper(), m.Groups[2].Value);
    });

If anybody can think of a better solution, then please let me know. Greatly appreciated!

  • 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-06-11T21:43:17+00:00Added an answer on June 11, 2026 at 9:43 pm

    MatchEvaluator is fine, think you burned me 😉 Anyway :

    var pattern = @"([a-z]+) ([a-z]+)";
    var format = "[starts-with({0}{1}]";
    
    var input = "bla bla";
    var result = ReplacePattern(input, pattern, format);
    
    public static string ReplacePattern(string input, string pattern, string format)
    {
       if (Regex.Match(input, pattern).Groups.Count != 3) return input;//or throw, or...
       return Regex.Replace(input, pattern, x =>
                string.Format(format,
                              x.Groups[1].Value.ToUpper(), 
                              x.Groups[2].Value));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I have the following regex: -(\d+)- and I want to replace, using
Suppose I have a regex like this: @\b(one|two|three)\b; How can I get all of
Suppose I have these line: /path-to-something/section/resource?var=name /path-to-something/section/resource I want to use regular expression to
I need to write a regex which will match URLs that do not have
Is there a convenient way to write a regex that will try to match
I have a regex pattern for URL's that I use to check for links
Suppose I have '/srv/www/site.com/htdocs/system/application/views/' and want to test it against a regexp that matches
Suppose I have a string like this: abc%\%%%% I want to replace multiple %%%
Suppose I have a pattern in the form of ((?<happy>foo)|(?<sad>bar)|...) . There could be
Suppose I have $('#routingNumber').removeClass(validationClass('#routingNumber')); (Where validationClass() is a regex that returns a class that

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.