开发者

What does the regular expression search/seplace pattern do? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

What does the <matcher>?() regular expression do when used in the context of a search replace?

string input = "z=""(?<matcher>([a-z]{3,15}))"""
string pattern = z="cat"
string replacement = @"<ANIMAL>$开发者_如何学编程{matcher}</ANIMAL>";
string formattedOutput = Regex.Replace(input, pattern, replacement);

The formattedOutput will be "cat" after the expression has evaluated.


You have a lot of errors...

Here is the correction:

        string pattern = @"z=\""(?<matcher>([a-z]{3,15}))\""";
        string input = @"z=""cat""";
        string replacement = @"<ANIMAL>${matcher}</ANIMAL>";
        string formattedOutput = Regex.Replace(input, pattern, replacement);

        Console.WriteLine(formattedOutput);

?<matcher> is just a named group. You can pick any name. For example the following is equivalent:

        string pattern = @"z=\""(?<WHATEVER>([a-z]{3,15}))\""";
        string input = @"z=""cat""";
        string replacement = @"<ANIMAL>${WHATEVER}</ANIMAL>";
        string formattedOutput = Regex.Replace(input, pattern, replacement);

        Console.WriteLine(formattedOutput);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜