Match and replace

后端 未结 5 2083
有刺的猬
有刺的猬 2020-12-08 14:03

I have a long string and within that string I have the following text:

\"formatter\": \"SomeInformationHere\"

I need to fi

5条回答
  •  臣服心动
    2020-12-08 15:06

    Everybody else has pretty much nailed it with using capturing groups and substitutions, just wanted to provide a little more context:

    The main two things that are used here are Named Capturing Groups and Substitutions

    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 = "(?

提交回复
热议问题