Regex.Replace Malfunction when inserting "$0"

Posted on 2005-03-24
Medium Priority
Last Modified: 2008-01-09
I have the following code:
string message = <@REVENUE_POTENTIAL@>;
Regex reg = new Regex(VAR_REVENUE_POTENTIAL);
message = reg.Replace(message, REPLACEMENT_STRING);

When REPLACEMENT_STRING = "$0 to $1K", after the Replacement is performed, message (bad) = "<@REVENUE_POTENTIAL@> to $1K"
When REPLACEMENT_STRING = something else (like "$1K to $5K, etc), then message (good) = "$1K to $5K"

For some reason, when REPLACEMENT_STRING begins with the characters $0, this part of the string is not replaced - instead, the rest of the replacement string is appended to the original text (which is not deleted). I have tried this with other digits (ie: strings beginning with $1, etc) and it works fine. It seems to only be happening when the replacement string begins with $0.

Why is this happening?
Question by:jellis613
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 17

Accepted Solution

Jesse Houwing earned 1400 total points
ID: 13623558
$number is a reserved combination used to insert numbered groups into a search result. To escape these you should double the '$'.


Just to explain how this works:

If your regex is like this: @"<@([^@]+)@>"" (which will match any of your include strings) with your example string:   @"\<\@REVENUE_POTENTIAL\@\>" then

$0 will be: the whole string: <@REVENUE_POTENTIAL@>
$1 will be: the part between the first set of braces: REVENUE_POTENTIAL

$$0 will just be $0
$$1 will just be $1

If there is no numbered group then $number will just remain $number.

Author Comment

ID: 13623656
I added the line:
if (REVENUE_POTENTIAL.Substring(0,3) == "$0 ") {
and it now works. Thanks!
LVL 17

Expert Comment

by:Jesse Houwing
ID: 13623844
It would be even better to just escape all the '$' with '$$' just in case the regex ever changes.

Author Comment

ID: 13643983
I have taken your advice and implemented a replacement of every single $ sign with $$, in all of my parameters:

Regex regMatchDollars = new Regex(@"\$"); // Matches any one dollar sign

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question