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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:49:49+00:00 2026-06-01T13:49:49+00:00

string input = Hello World!; string pattern = (World|Universe); string replacement = $1; string

  • 0
string input = "Hello World!";
string pattern = "(World|Universe)";
string replacement = "$1";

string result = Regex.Replace(input, pattern, replacement);

Having the following example, the result would be "Hello World!", as the $1 gets replaced with the first group (World|Universe), however the result I want is "Hello $1!"

The Regex.Escape method is meant to be used to escape a Regex pattern, not the replacement, as it can escape other characters like slashes and other Regex pattern characters. The obvious fix to my problem is to have my replacement equal to "$$1", and will achieve "Hello $1!", but I was wondering if the dollar sign is the only value I have to escape (assuming replacement is user generated, and I do not know it ahead of time), or is there a helper function that does this already.

Does anyone know of a function to escape the replacement value that Regex.Replace(string input, string pattern, string replacement) uses?

  • 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-01T13:49:50+00:00Added an answer on June 1, 2026 at 1:49 pm

    From MSDN:

    The replacement parameter specifies the string that is to replace each match in input. replacement can consist of any combination of literal text and substitutions.

    The following substitutions are defined:

    • $number
    • ${name}
    • $$
    • $&
    • $`
    • $’
    • $+
    • $_

    Substitutions are the only special constructs recognized in a replacement pattern. None of the other regular expression language elements, including character escapes and the period (.), which matches any character, are supported. Similarly, substitution language elements are recognized only in replacement patterns and are never valid in regular expression patterns.

    So it look like it’s only the $ character that needs to be escaped.

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

Sidebar

Related Questions

Given the following code : .section .rodata str: .string "Hello World!\n" input: .long 2
Given the following form: <form> <input name=foo value=bar> <input name=hello value=hello world> </form> I
#include <string> std::string input; std::cin >> input; The user wants to enter Hello World.
If I have the following form: <form> <input name=foo value=bar> <input name=hello value=world> <input
scala> val input = readLine(hello %s%n, world) hello WrappedArray(world) input: String = scala> val
I need a Hello World example... [WebService(Namespace = xxxxx)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService()] public
I have an input string that will follow the pattern /user/<id>?name=<name> , where <id>
I am going to have a string like hello world as a hashmap key.
How do i parse tokens from an input string. For example: char *aString =
Hi i am trying to create an application1 which takes input data hello world.

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.