问题:
找不到一致的方法来用我想要的特定字符串替换引号之间的随机字符串。任何帮助将不胜感激。
例子:
String str1 = "test=\"-1\"";
应该成为
String str2 = "test=\"31\"";
但也为
String str3 = "test=\"foobar\"";
基本上我想把这个
String str4 = "test=\"antyhingCanGoHere\"";
进入这个
String str4 = "test=\"31\"";
试过:
不使用 RegexOptions 枚举的不区分大小写的正则表达式
使用正则表达式将 AnyText: 和 <usernameredacted@example.com> 之间的任何字符替换为空字符串?
当前代码:
Regex RemoveName = new Regex("(?VARIABLE=\").*(?=\")", RegexOptions.IgnoreCase);
String convertSeccons = RemoveName.Replace(ruleFixed, "31");
返回错误:
System.ArgumentException was caught
Message=parsing "(?VARIABLE=").*(?=")" - Unrecognized grouping construct.
Source=System
StackTrace:
at System.Text.RegularExpressions.RegexParser.ScanGroupOpen()
at System.Text.RegularExpressions.RegexParser.ScanRegex()
at System.Text.RegularExpressions.RegexParser.Parse(String re, RegexOptions op)
at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options, Boolean useCache)
at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options)
at application.application.insertGroupID(String rule) in C:\Users\winserv8\Documents\Visual Studio 2010\Projects\application\application\MainFormLauncher.cs:line 298
at application.application.xmlqueryDB(String xmlSaveLocation, TextWriter tw, String ruleName) in C:\Users\winserv8\Documents\Visual Studio 2010\Projects\application\application\MainFormLauncher.cs:line 250
InnerException:
找到答案
string s = Regex.Replace(ruleFixed, "VARIABLE=\"(.*)\"", "VARIABLE=\"31\"");
ruleFixed = s;
我在 Replace any character in between AnyText: and with an empty string using Regex? 找到了这个代码示例?这是我之前发布的链接之一,只是跳过了这个语法,因为我认为它不能处理我需要的东西。