Regex.Match for a string like sampleText(any Alphanumeric ch

本文关键字:any Alphanumeric ch sampleText like Match for string Regex | 更新日期: 2023-09-27 18:32:55

我有一个字符串

"AND A.BrandId IN (0,1,3,5,9,10) AND AgeGroup IN (0-24,24-36) AND Gender IN (0,1,2)"

我想用其他文本替换"AgeGroup IN (0-24,24-36)",保持字符串的其余部分不变。怎么做?

Regex.Match for a string like sampleText(any Alphanumeric ch

为此使用

String.Replace("Old String", "New String") 方法比使用正则表达式要容易得多:

例:

string oldFoo = "AND A.BrandId IN (0,1,3,5,9,10) AND AgeGroup IN (0-24,24-36) AND Gender IN (0,1,2)";
string newFoo;
newFoo = oldFoo.Replace("AgeGroup IN (0-24,24-36)", "newFooText");


有关 String.Replace() 方法的更多信息: MSDN

如果您认为您的字符串可能会更改为 AgeGroup IN (0-2, 0-4, 24-56,24-36, 4-12) 类似的东西,您可以使用正则表达式将此字符串替换为其他字符串。

                string newString = Regex.Replace(str, @"AgeGroup'sIN's'(('s*'d*[-]'d*,*)*')", "Other_Text");

你可以看看这里。