如何在Regex中进行匹配评估。更换工作?

时间:2021-07-10 05:39:54

This is the input string 23x * y34x2. I want to insert " * " (star surrounded by whitespaces) after every number followed by letter, and after every letter followed by number. So my input string would look like this: 23 * x * y * 34 * x * 2.

这是输入字符串23x * y34x2。我想在每个数字后面跟字母,在每个字母后面跟数字后面加上“*”(由白色空格包围的星号)。输入字符串是这样的:23 * x * y * 34 * x * 2。

This is the regex that does the job: @"\d(?=[a-z])|[a-z](?=\d)". This is the function that I wrote that inserts the " * ".

这个正则表达式的工作:@“\ d(? =[a - z])|[a - z](? = \ d)”。这是我编写的插入“*”的函数。

Regex reg = new Regex(@"\d(?=[a-z])|[a-z](?=\d)");   
MatchCollection matchC;
matchC = reg.Matches(input);
int ii = 1;
foreach (Match element in matchC)//foreach match I will find the index of that match
{
    input = input.Insert(element.Index + ii, " * ");//since I' am inserting " * " ( 3 characters )
    ii += 3;                                        //I must increment index by 3
}
return input; //return modified input

My question how to do same job using .net MatchEvaluator? I'am new to regex and don't understand good replacing with MatchEvaluator. This is the code that I tried to wrote:

我的问题是如何使用。net MatchEvaluator来做同样的工作?我是regex的新手,不明白用MatchEvaluator代替是什么意思。这是我试着写的代码:

    Regex reg = new Regex(@"\d(?=[a-z])|[a-z](?=\d)");
    MatchEvaluator matchEval = new MatchEvaluator(ReplaceStar);
    input = reg.Replace(input, matchEval);
    return input;
}
public string ReplaceStar( Match match )
{
    //return What??
}

1 个解决方案

#1


53  

A MatchEvaluator is a delegate that takes a Match object and returns a string that should be replaced instead of the match. You can also refer to groups from the match. You can rewrite your code as follows:

MatchEvaluator是一个委托,它接受Match对象并返回一个应该替换而不是匹配的字符串。您还可以引用来自匹配的组。您可以重写代码如下:

string input = "23x * y34x2";
Regex reg = new Regex(@"\d(?=[a-z])|[a-z](?=\d)");
string result = reg.Replace(input, delegate(Match m) {
    return m.Value + " * ";
});

To give an example of how this works, the first time the delegate is called, Match parameter will be a match on the string "3". The delegate in this case is defined to return the match itself as a string concatenated with " * ". So the first "3" is replaced with "3 * ".

为了举例说明该如何工作,第一次调用委托时,Match参数将是字符串“3”上的匹配。在本例中,委托被定义为将匹配本身作为与“*”连接的字符串返回。所以第一个3被3 *取代。

The process continues in this way, with delegate being called once for each match in the original string.

这个过程以这种方式继续,在原始字符串中,每个匹配都被调用一次。

#1


53  

A MatchEvaluator is a delegate that takes a Match object and returns a string that should be replaced instead of the match. You can also refer to groups from the match. You can rewrite your code as follows:

MatchEvaluator是一个委托,它接受Match对象并返回一个应该替换而不是匹配的字符串。您还可以引用来自匹配的组。您可以重写代码如下:

string input = "23x * y34x2";
Regex reg = new Regex(@"\d(?=[a-z])|[a-z](?=\d)");
string result = reg.Replace(input, delegate(Match m) {
    return m.Value + " * ";
});

To give an example of how this works, the first time the delegate is called, Match parameter will be a match on the string "3". The delegate in this case is defined to return the match itself as a string concatenated with " * ". So the first "3" is replaced with "3 * ".

为了举例说明该如何工作,第一次调用委托时,Match参数将是字符串“3”上的匹配。在本例中,委托被定义为将匹配本身作为与“*”连接的字符串返回。所以第一个3被3 *取代。

The process continues in this way, with delegate being called once for each match in the original string.

这个过程以这种方式继续,在原始字符串中,每个匹配都被调用一次。