I have a string as
"AND A.BrandId IN (0,1,3,5,9,10) AND AgeGroup IN (0-24,24-36) AND Gender IN (0,1,2)"
and I want to replace the "AgeGroup IN (0-24,24-36)"
with other text, keeping the remaining part of the string as it as. How to do that?
使用该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");
如果您认为您的字符串可能会更改为 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");
你可以看看这里。