too*_*are 12 c# regex string replace
问题:
无法找到一种一致的方法来替换具有我想要的特定字符串的引号之间的随机字符串.任何帮助将不胜感激.
例:
String str1 = "test=\"-1\"";
Run Code Online (Sandbox Code Playgroud)
应该成为
String str2 = "test=\"31\"";
Run Code Online (Sandbox Code Playgroud)
但也为之努力
String str3 = "test=\"foobar\"";
Run Code Online (Sandbox Code Playgroud)
基本上我想转此
String str4 = "test=\"antyhingCanGoHere\"";
Run Code Online (Sandbox Code Playgroud)
进入这个
String str4 = "test=\"31\"";
Run Code Online (Sandbox Code Playgroud)
试过:
不使用RegexOptions枚举的不区分大小写的正则表达式
使用Regex将空字符串替换为AnyText:和<usernameredacted@example.com>之间的任何字符?
当前代码:
Regex RemoveName = new Regex("(?VARIABLE=\").*(?=\")", RegexOptions.IgnoreCase);
String convertSeccons = RemoveName.Replace(ruleFixed, "31");
Run Code Online (Sandbox Code Playgroud)
返回错误:
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:
Run Code Online (Sandbox Code Playgroud)
找到答案
string s = Regex.Replace(ruleFixed, "VARIABLE=\"(.*)\"", "VARIABLE=\"31\"");
ruleFixed = s;
Run Code Online (Sandbox Code Playgroud)
我发现这个代码示例在AnyText:和使用Regex的空字符串之间替换任何字符?这是我之前发布的链接之一,只是跳过了这种语法,因为我认为它无法处理我需要的东西.
string s = Regex.Replace(ruleFixed, "VARIABLE=\"(.*)\"", "VARIABLE=\"31\"");
ruleFixed = s;
Run Code Online (Sandbox Code Playgroud)
我在使用正则表达式将 AnyText: 和 <usernameredacted@example.com> 之间的任何字符替换为空字符串中找到了此代码示例?这是我之前发布的链接之一,只是跳过了这个语法,因为我认为它无法处理我需要的内容。