C#正则表达式:如何提取$ 1,$ 2个变量来自比赛 [英] C# Regexp: How to extract $1, $2 variables from match
问题描述
目前.NET使用自己的非标准捕获组命名规则是一只狗。 我想,以使标准使用$ 1,$ 2捕获组在C#。
Currently .NET uses their own nonstandard capturing group naming convention which is a dog. I would like to enable the standard use of $1, $2 capture groups in C#.
时他们没有办法做的,如果还是不行,是他们可以使用任何第三方的正则表达式引擎,做实现了这种功能。
Is their any way of doing it, or if not, is their any thirdparty regexp engines available for use, which do implement that kind of functionality.
推荐答案
这不是100%清楚你在找什么,但它听起来就像你想要得到的值给定组中的匹配常规的前$的能力p $ pssion。这是绝对有可能在C#(和.NET一般)。
It's not 100% clear what you're looking for but it sounds like you want the ability to get the value for a given group in a matched regular expression. This is definitely possible in C# (and .Net in general).
例如。
var regex = new Regex(@"(a+)(\d+)");
var match = regex.Match("a42");
Console.WriteLine(match.Groups[1].Value); // Prints a
Console.WriteLine(match.Groups[2].Value); // Prints 42
虽然我不经常使用单声道,我会感到非常惊讶,如果这没有工作,有作为。
While I don't use Mono regularly, I would be very surprised if this didn't work there as well.
这篇关于C#正则表达式:如何提取$ 1,$ 2个变量来自比赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!