?
Solved

Regex.Replace Malfunction when inserting "$0"

Posted on 2005-03-24
4
Medium Priority
?
726 Views
Last Modified: 2008-01-09
I have the following code:
string message = <@REVENUE_POTENTIAL@>;
const string VAR_REVENUE_POTENTIAL  = @"\<\@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?
0
Comment
Question by:jellis613
[X]
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
4 Comments
 
LVL 17

Accepted Solution

by:
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 '$'.

REPLACEMENT_STRING = "$$0 to $$1K"

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

but:
$$0 will just be $0
$$1 will just be $1

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

Author Comment

by:jellis613
ID: 13623656
I added the line:
if (REVENUE_POTENTIAL.Substring(0,3) == "$0 ") {
      REVENUE_POTENTIAL = "$" + REVENUE_POTENTIAL;
}
and it now works. Thanks!
0
 
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.
0
 
LVL 2

Author Comment

by:jellis613
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
...
REPLACEMENT_STRING = regMatchDollars.Replace(REPLACEMENT_STRING,"$$$$");
0

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