Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取找到匹配项的原始正则表达式模式_C#_.net_Regex - Fatal编程技术网

C# 获取找到匹配项的原始正则表达式模式

C# 获取找到匹配项的原始正则表达式模式,c#,.net,regex,C#,.net,Regex,假设我有:Match regexMatch=regex.Match(lineToScan) 正则表达式是正则表达式模式的对象(例如A | B | C) 因此,我可以找到马赫数是针对哪个正则表达式(对于A或对于B等)找到的吗?您应该使用,因为这将返回集合中的所有匹配项 foreach (Match m in Regex.Matches(value, pattern)) Console.WriteLine(m.Value); 您可以使用组来确定正则表达式的哪一部分匹配: var regex

假设我有:
Match regexMatch=regex.Match(lineToScan)
正则表达式是正则表达式模式的对象(例如A | B | C)
因此,我可以找到马赫数是针对哪个正则表达式(对于A或对于B等)找到的吗?

您应该使用,因为这将返回集合中的所有匹配项

foreach (Match m in Regex.Matches(value, pattern))
    Console.WriteLine(m.Value);

您可以使用组来确定正则表达式的哪一部分匹配:

var regex = new Regex("(?<a>A)|(?<b>B)|(?<c>C)");
var match = regex.Match("B");
var matchesA = match.Groups["a"].Success; // will be false
var matchesB = match.Groups["b"].Success; // will be true
var matchesC = match.Groups["c"].Success; // will be false
var regex=新的regex((?A)|(?B)|(?C));
var match=regex.match(“B”);
var matchesA=match.Groups[“a”]。成功;//将是错误的
var matchesB=match.Groups[“b”].Success;//这将是真的
var matchesC=match.Groups[“c”].Success;//将是错误的

使用命名组为每个部件命名(“a”、“b”和“c”)。然后,您可以检查
Groups
属性,以发现哪个组(如果有)成功匹配。

如果使用单词,则需要两个匹配项:

string regex_string = "WORD_0|WORD_1|WORD_N";
Regex regex_matcher = new Regex(@"(" + regex_string + @")\b", RegexOptions.Multiline | RegexOptions.IgnoreCase);

string result = regex_matcher.Replace("This return 'word_1' in UpperCase", m => Regex.Match(regex_string, m.ToString(), RegexOptions.IgnoreCase | RegexOptions.Multiline).Value);

听起来不错,但唯一的问题是我正在动态地形成正则表达式模式。。因此,无法将命名集合用于组捕获。有没有办法从匹配对象中获取它?