What does the regular expression search/seplace pattern do? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this questionWhat 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);
精彩评论