匹配和替换

Har*_*rag 28 c# regex

我有一个长字符串,该字符串中我有以下文字:

"formatter": "SomeInformationHere"
Run Code Online (Sandbox Code Playgroud)

我需要在长字符串中找到上面的文本并删除SomeInformationHere周围的双引号,所以结果如下所示,但是"formatter"这个词的引号必须保留.

"formatter": SomeInformationHere
Run Code Online (Sandbox Code Playgroud)

我尝试了下面的,找到了字符串,但我不知道如何只更换值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);
Run Code Online (Sandbox Code Playgroud)

编辑:哦,我忘了提到上面的模式可能不止一次在"mystring"中,并且所有人都需要替换它们.


编辑2:

我已经看过正则表达式测试站点(感谢链接)并粘贴在我的测试字符串和正则表达式模式中,它似乎可以工作,但当我将相同的模式放入点网时,替换似乎工作为如果选择了"单行"选项.以下是我用过的代码.

  1. 字符串 - 请注意,这不包含任何回车符 - 它是一个从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;}"
            }
    }
    
    Run Code Online (Sandbox Code Playgroud)

正如你可以看到"test"和"tooltip"旁边的底部附近,我有"formatter:"部分.我正在使用的模式当上面的字符串全部在测试器中的几行(带有CR)时工作,但是当我把它放在一行就像它应该是那样模式不起作用

我正在使用的.NET代码/模式是:

string pattern = "(\"formatter\": )\"(.*)\"( })";
var regex = new Regex(pattern, RegexOptions.IgnoreCase);
aJSON = regex.Replace(aJSON, "$1$2$3");
Run Code Online (Sandbox Code Playgroud)

再次感谢.但我仍然无法让模式在测试仪中正常工作.

REGex测试站点中的目标字符串:(没有CR)

{"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;}" } }
Run Code Online (Sandbox Code Playgroud)

我现在已经找到了正确的模式,似乎可以工作并在字符串中找到多个匹配项.在此处发布以完成.

string pattern ="(\"formatter \":)\"(.[^ \"]*)\"";

Jos*_*osh 28

其他人几乎已经使用捕获组和替换来钉它,只是想提供更多的上下文:

这里使用的主要两件事是命名捕获组替换

static void Main(string[] args) {

    var input = new[] {
        "\"formatter\": \"John\"", 
        "\"formatter\": \"Sue\"", 
        "\"formatter\": \"Greg\""
    };

    foreach (var s in input) {
        System.Console.Write("Original: [{0}]{1}", s, Environment.NewLine);
        System.Console.Write("Replaced: [{0}]{1}", ReFormat(s), Environment.NewLine);
        System.Console.WriteLine();
    }

    System.Console.ReadKey();
}

private static String ReFormat(String str) {
    //Use named capturing groups to make life easier
    var pattern = "(?<label>\"formatter\"): ([\"])(?<tag>.*)([\"])";

    //Create a substitution pattern for the Replace method
    var replacePattern = "${label}: ${tag}";

    return Regex.Replace(str, pattern, replacePattern, RegexOptions.IgnoreCase);
}
Run Code Online (Sandbox Code Playgroud)


rus*_*sty 16

您可以像这样使用Regex.Replace方法:

string pattern = "\"formatter\": \"(.*)\"";
myString = Regex.Replace(myString, pattern, "\"formatter\": $1");
Run Code Online (Sandbox Code Playgroud)