我有一個很長的字符串和內該字符串我有以下文字:匹配和替換
"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