简体   繁体   中英

How to perform multiple Regex replacements in sequence from a list of unique items cleanly in C#

I'm trying to find a cleaner way of performing multiple sequential replacements on a single string where each replacement has a unique pattern and string replacement.

For example, if I have 3 pairs of patterns-substitutions strings:

1. /(?<,\\)\\n/, "\n"

2. /(\\)(?=[\;\:\,])/, ""

3. /(\\{2})/, "\\"

I want to apply regex replacement 1 on the original string, then apply 2 on the output of 1, and so on and so forth.

The following console program example does exactly what I want, but it has a lot of repetition, I am looking for a cleaner way to do the same thing.

SanitizeString

static public string SanitizeString(string param)
{
    string retval = param;
    //first replacement
    Regex SanitizePattern = new Regex(@"([\\\;\:\,])");
    retval = SanitizePattern.Replace(retval, @"\$1");

    //second replacement
    SanitizePattern = new Regex(@"\r\n?|\n");
    retval = SanitizePattern.Replace(retval, @"\n");

    return retval;
}

ParseCommands

static public string ParseCommands(string param)
{
    string retval = param;
    //first replacement
    Regex SanitizePattern = new Regex(@"(?<!\\)\\n");
    retval = SanitizePattern.Replace(retval, System.Environment.NewLine);

    //second replacement
    SanitizePattern = new Regex(@"(\\)(?=[\;\:\,])");
    retval = SanitizePattern.Replace(retval, "");

    //third replacement
    SanitizePattern = new Regex(@"(\\{2})");
    retval = SanitizePattern.Replace(retval, @"\");

    return retval;
}

Main

using System;
using System.IO;
using System.Text.RegularExpressions;

...

static void Main(string[] args)
{
    //read text that contains user input
    string sampleText = File.ReadAllText(@"c:\sample.txt");

    //sanitize input with certain rules
    sampleText = SanitizeString(sampleText);
    File.WriteAllText(@"c:\sanitized.txt", sampleText);

    //parses escaped characters back into the original text
    sampleText = ParseCommands(sampleText);
    File.WriteAllText(@"c:\parsed_back.txt", sampleText);
}

Don't mind the file operations. I just used that as a quick way to visualize the actual output. In my program I'm going to use something different.

Here's one way:

var replacements = new List<(Regex regex, string replacement)>()
{
    (new Regex(@"(?<!\\)\\n"), System.Environment.NewLine),
    (new Regex(@"(\\)(?=[\;\:\,])"), ""),
    (new Regex(@"(\\{2})"), @"\"),
};

(Ideally cache that in a static readonly field):

Then:

string retval = param;
foreach (var (regex, replacement) in replacements)
{
    retval = regex.Replace(retval, replacement);
}

Or you could go down the linq route:

string retval = replacements
    .Aggregate(param, (str, x) => x.regex.Replace(str, x.replacement));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM