如何在 C# 中从唯一项目列表中按顺序执行多个正则表达式替换
How to perform multiple Regex replacements in sequence from a list of unique items cleanly in C#
我正在尝试找到一种更简洁的方法来对单个字符串执行多个顺序替换,其中每个替换都有唯一的模式和字符串替换。
例如,如果我有 3 对模式替换字符串:
1. /(?<!\)\n/, "\n"
2. /(\)(?=[\;\:\,])/, ""
3. /(\{2})/, "\"
我想在原始字符串上应用正则表达式替换 1,然后在 1 的输出上应用 2,依此类推。
下面的控制台程序示例完全符合我的要求,但它有很多重复,我正在寻找一种更简洁的方法来做同样的事情。
清理字符串
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;
}
解析命令
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;
}
主要
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);
}
不介意文件操作。我只是将其用作可视化实际输出的快速方法。在我的程序中,我将使用不同的东西。
这是一种方法:
var replacements = new List<(Regex regex, string replacement)>()
{
(new Regex(@"(?<!\)\n"), System.Environment.NewLine),
(new Regex(@"(\)(?=[\;\:\,])"), ""),
(new Regex(@"(\{2})"), @"\"),
};
(理想情况下将其缓存在静态只读字段中):
然后:
string retval = param;
foreach (var (regex, replacement) in replacements)
{
retval = regex.Replace(retval, replacement);
}
或者你可以走 linq 路线:
string retval = replacements
.Aggregate(param, (str, x) => x.regex.Replace(str, x.replacement));
我正在尝试找到一种更简洁的方法来对单个字符串执行多个顺序替换,其中每个替换都有唯一的模式和字符串替换。
例如,如果我有 3 对模式替换字符串:
1. /(?<!\)\n/, "\n"
2. /(\)(?=[\;\:\,])/, ""
3. /(\{2})/, "\"
我想在原始字符串上应用正则表达式替换 1,然后在 1 的输出上应用 2,依此类推。
下面的控制台程序示例完全符合我的要求,但它有很多重复,我正在寻找一种更简洁的方法来做同样的事情。
清理字符串
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;
}
解析命令
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;
}
主要
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);
}
不介意文件操作。我只是将其用作可视化实际输出的快速方法。在我的程序中,我将使用不同的东西。
这是一种方法:
var replacements = new List<(Regex regex, string replacement)>()
{
(new Regex(@"(?<!\)\n"), System.Environment.NewLine),
(new Regex(@"(\)(?=[\;\:\,])"), ""),
(new Regex(@"(\{2})"), @"\"),
};
(理想情况下将其缓存在静态只读字段中):
然后:
string retval = param;
foreach (var (regex, replacement) in replacements)
{
retval = regex.Replace(retval, replacement);
}
或者你可以走 linq 路线:
string retval = replacements
.Aggregate(param, (str, x) => x.regex.Replace(str, x.replacement));