简体   繁体   中英

Prevent replacing an already replaced string in comma-separated string

I have a string which is

string mainstr = "NONSALE_REVENUE,SALE_REVENUE";

I am trying to replace NONSALE_REVENUE with SUM(NONSALE_REVENUE) and SALE_REVENUE with SUM(SALE_REVENUE)

What I have tried is :

 mainstr = mainstr.Replace("SALE_REVENUE", "SUM(SALE_REVENUE)");
 mainstr = mainstr.Replace("NONSALE_REVENUE", "SUM(NONSALE_REVENUE)");

Which is giving me wrong result:

NONSUM(SALE_REVENUE,SUM(SALE_REVENUE

My Expected result is SUM(NONSALE_REVENUE),SUM(SALE_REVENUE)

How can I achieve this ?

You could use the , char to detect the single values

string mainstr = "NONSALE_REVENUE,SALE_REVENUE";
string result = string.Join(",", mainstr.Split(',').Select(x => "SUM("+ x + ")"));

You can achieve this with RegEx

string mainstr = "NONSALE_REVENUE,SALE_REVENUE";
string newString = Regex.Replace(mainstr, @"\bNONSALE_REVENUE\b", "SUM(NONSALE_REVENUE)");
newString = Regex.Replace(newString, @"\bSALE_REVENUE\b", "SUM(SALE_REVENUE)");

Console.WriteLine(newString);

A single regex replacement step:

Regex.Replace(mainstr, @"(?:NON)?SALE_REVENUE", "SUM($&)")

If you want to ensure you replace a whole word, enclose the pattern with \\b (word boundaries):

Regex.Replace(mainstr, @"\b(?:NON)?SALE_REVENUE\b", "SUM($&)")

Or, if it is important to only match a substring between commas and/or at the start/end of string , use

Regex.Replace(mainstr, @"(?<![^,])(?:NON)?SALE_REVENUE(?![^,])", "SUM($&)")

where (?<![^,]) is a negative lookbehind that requires no non-comma char immediately to the left and the (?![^,]) is a negative lookahead that requires no non-comma char immediately to the right of the search phrase.

The $& in the replacement pattern inserts the whole match during the replacement.

See the regex demo online .

Pattern details

  • \\b - a word boundary or
  • (?<![^,]) - a negative lookbehind that requires no non-comma char immediately to the left
  • (?:NON)? - an optional non-capturing group matching 1 or 0 occurrences of NON
  • SALE_REVENUE - a literal substring
  • (?![^,]) - a negative lookahead that requires no non-comma char immediately to the right of the search phrase or
  • \\b - (closing) word boundary.

在此输入图像描述

See the C# demo :

string mainstr = "NONSALE_REVENUE,SALE_REVENUE";
string result = Regex.Replace(mainstr, @"(?<![^,])(?:NON)?SALE_REVENUE(?![^,])", "SUM($&)");
Console.WriteLine(result);

If mainstr is always going to be "NONSALE_REVENUE,SALE_REVENUE" , then use the comma as part of your replace operation, so use this:

mainstr = mainstr.Replace(",SALE_REVENUE", ",SUM(SALE_REVENUE)");
mainstr = mainstr.Replace("NONSALE_REVENUE,", "SUM(NONSALE_REVENUE),");

Using Regex.Replace :

string mainstr = "NONSALE_REVENUE,SALE_REVENUE";
string result = Regex.Replace(mainstr, @"\b\w+\b", "SUM($0)");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM