且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# - 在分隔符之间删除文本字符串 - 正则表达式?

更新时间:2023-02-17 22:52:12

简单的正则表达式是:

string input = "Give [Me Some] Purple (And More) Elephants";
string regex = "(\\[.*\\])|(\".*\")|('.*')|(\\(.*\\))";
string output = Regex.Replace(input, regex, "");

至于这样做,你想建立的正则表达式你只需要建立各部分的自定义方式:

As for doing it a custom way where you want to build up the regex you would just need to build up the parts:

('.*')  // example of the single quote check

然后让每个人的正则表达式部分连接而成OR(该|在正则表达式)在我原来的例子。一旦你有你的正则表达式的字符串内置只运行一次。关键是让正则表达式到一个单一的检查,因为执行上的一个项目一个多正则表达式匹配,然后通过大量的项目迭代可能会看到在性能显著下降。

Then have each individual regex part concatenated with an OR (the | in regex) as in my original example. Once you have your regex string built just run it once. The key is to get the regex into a single check because performing a many regex matches on one item and then iterating through a lot of items will probably see a significant decrease in performance.

在我的第一个例子中,将采取以下行的地方:

In my first example that would take the place of the following line:

string input = "Give [Me Some] Purple (And More) Elephants";
string regex = "Your built up regex here";
string sOutput = Regex.Replace(input, regex, "");

我相信有人会发表一个很酷的LINQ EX pression根据分隔符对象的数组来匹配或东西来构建正则表达式。

I am sure someone will post a cool linq expression to build the regex based on an array of delimiter objects to match or something.