我有一个很长的字符串和内该字符串我有以下文字:匹配和替换
"formatter": "SomeInformationHere"
我需要找到一长串内上面的文字,并删除周围双引号SomeInformationHere所以结果如下图所示,但“格式化程序”一词的引号必须保留。
"formatter": SomeInformationHere
我试过以下,它找到了这个字符串,但我不知道如何更换围绕价值SomeInformationHere只是引号:
string pattern = "\"formatter\": ([\"]).*([\"])";
Match match = Regex.Match(myString, pattern, RegexOptions.IgnoreCase);
//Replace text in "myString" here
myString = ?????
//Output match value:
Response.Write(match.Value);
编辑:哦,我忘了提到上面的模式可能不止一次地在“mystring”中,并且所有的都需要替换它们。
编辑2:
我看了一下正则表达式测试网站(感谢您的链接),并在我的测试字符串和正则表达式粘贴,似乎在工作,但是当我把相同的模式放入网络中,替换似乎就像选择了“单行”选项一样工作。以下是我用过的代码。
字符串 - 注意,这不包含任何回车 - 它已经从一个XML文件中建一个长字符串。格式化以提高可读性。
{ "chart": { "borderRadius": 15, "borderWidth": 1, "renderTo": "ChartContainer1", "type": "pie" }, "credits": { "enabled": false }, "labels": { "items": [{ "html": "Label 1", "style": { "left": "10px", "top": "30px" } }, { "html": "Label 2", "style": { "left": "10px", "top": "50px" } }, { "dummy": null }] }, "plotOptions": { "pie": { "allowPointSelect": true, "cursor": "pointer", "showInLegend": true } }, "series": [{ "data": [{ "name": "Firefox", "y": 45.0 }, { "name": "IE", "y": 26.8 }, { "name": "Chrome", "selected": true, "sliced": true, "y": 12.8 }, { "name": "Safari", "y": 8.5 }, { "name": "Opera", "y": 6.2 }, { "name": "Others", "y": 0.7 }], "name": "Browser share" }, { "dummy": null }], "test": { "formatter": "function(){return \u0027\u0027+ this.point.name +\u0027<\/b>: \u0027+ this.y +\u0027 %\u0027;}" }, "title": { "align": "center", "text": "Your chart title here" }, "tooltip": { "formatter": "function(){return \u0027\u0027+ this.point.name +\u0027<\/b>: \u0027+ this.y +\u0027 %\u0027;}" } }
正如你可以靠近底部旁边的“测试”和“工具提示”看我有“格式化”的一部分。我正在使用的图案当上面的字符串全部在测试仪中的几行(带有CR)时,但是当我把它放在一条线上时它应该是那么图案不起作用
。我使用的NET代码/模式是:
string pattern = "(\"formatter\":)\"(.*)\"(})";
var regex = new Regex(pattern, RegexOptions.IgnoreCase);
aJSON = regex.Replace(aJSON, "$1$2$3");
再次感谢。但我仍然无法使模式在测试仪中正常工作。
目标字符串中的正则表达式测试网站(无CRS)
{"chart": {"borderRadius": 15, "borderWidth": 1, "renderTo": "ChartContainer1", "type": "pie" }, "credits": {"enabled": false }, "labels": { "items": [ {"html": "Label 1", "style": {"left": "10px", "top": "30px" } }, {"html": "Label 2", "style": {"left": "10px", "top": "50px" } }, {"dummy": null } ] }, "plotOptions": {"pie": {"allowPointSelect": true, "cursor": "pointer", "showInLegend": true } }, "series": [ { "data": [ {"name": "Firefox", "y": 45.0 }, {"name": "IE", "y": 26.8 }, {"name": "Chrome", "selected": true, "sliced": true, "y": 12.8 }, {"name": "Safari", "y": 8.5 }, {"name": "Opera", "y": 6.2 }, {"name": "Others", "y": 0.7 } ], "name": "Browser share" }, {"dummy": null } ], "test": {"formatter": "function(){return \u0027\u0027+ this.point.name +\u0027<\/b>: \u0027+ this.y +\u0027 %\u0027;}" }, "title": {"align": "center", "text": "Your chart title here" }, "tooltip": {"formatter": "function(){return \u0027\u0027+ this.point.name +\u0027<\/b>: \u0027+ this.y +\u0027 %\u0027;}" } }
我现在发现,似乎工作,并找到字符串中的多个匹配正确的模式。在这里发布完成。
串图案= “(\” 格式化\ “:)\”(。[^ \ “] *)\”“;
是否总是与'“格式化”开始'也可以是完全不同的东西? – 2011-05-05 15:08:26
http://regexhero.net/tester/是一个很棒的工具来测试正则表达式。如果你还不知道。 – 2011-05-05 15:23:09