简体   繁体   English

防止用逗号分隔的字符串替换已经替换的字符串

[英]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) 我试图取代NONSALE_REVENUESUM(NONSALE_REVENUE)SALE_REVENUESUM(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) 我的预期结果是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 您可以使用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): 如果要确保替换整个单词,请用\\b (单词边界)将模式括起来:

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. 其中(?<![^,])是一个负面的lookbehind,它不需要在左边立即使用非逗号字符,而(?![^,])是一个负向前瞻,它不需要立即向右的非逗号字符串搜索短语。

The $& in the replacement pattern inserts the whole match during the replacement. 替换模式中的$&在替换期间插入整个匹配。

See the regex demo online . 在线查看正则表达式演示

Pattern details 图案细节

  • \\b - a word boundary or \\b - 单词边界或
  • (?<![^,]) - a negative lookbehind that requires no non-comma char immediately to the left (?<![^,]) - 一个负面的lookbehind,它不需要在左边立即使用非逗号字符
  • (?:NON)? - an optional non-capturing group matching 1 or 0 occurrences of NON - 可选的非捕获组,匹配1或0次NON
  • SALE_REVENUE - a literal substring SALE_REVENUE - 文字子字符串
  • (?![^,]) - a negative lookahead that requires no non-comma char immediately to the right of the search phrase or (?![^,]) - 一个负向前瞻,在搜索短语的右边不需要非逗号字符或
  • \\b - (closing) word boundary. \\b - (结束)字边界。

在此输入图像描述

See the C# demo : 查看C#演示

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 总是 "NONSALE_REVENUE,SALE_REVENUE" ,那么使用逗号作为替换操作的一部分,所以使用:

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

Using Regex.Replace : 使用Regex.Replace

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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