非捕获组以C#显示

时间:2017-05-23 15:28:47

标签: c# regex

以下行

DCS120170517220207-FIC-023.FLW  07-FIC-023    00060Y000000011.266525G

我正在努力争取约会。但非捕获组似乎被忽略了。结果如下。我如何才能获得约会?

DCS1
DCS120170517 //This should be just 20170517
220207-FIC-023.FLW
07-FIC-023
Y
G

...

public static void parsePlainFile(string line)
{
    string patDcsName   = @"DCS[1-5]|DCSG";
    string patDate      = @"(?:^.{4})([2-9][0-9]{3}[0-1][0-9][0-3][0-9])";
    string patTagName   = @"[0-9]{6}-[A-Z]{2,4}-[0-Z]{2,4}\.[0-Z]{3}";
    string patRodsName  = @"(?<=\s)[0-9]{2}\-[A-Z]{3}\-[0-9]{3}(?=\s)";
    string patFreq      = @"(?<=\d{5})(Y|N)(?=\d{9})";
    string patUnit      = @"\w$";

    Match m = Regex.Match(line, patDcsName);
    Console.WriteLine(m.Value);
     m = Regex.Match(line, patDate);
    Console.WriteLine(m.Value);
     m = Regex.Match(line, patTagName);
    Console.WriteLine(m.Value);
     m = Regex.Match(line, patRodsName);
    Console.WriteLine(m.Value);
     m = Regex.Match(line, patFreq);
    Console.WriteLine(m.Value);
     m = Regex.Match(line, patUnit);
    Console.WriteLine(m.Value);

}

1 个答案:

答案 0 :(得分:5)

代码忽略了捕获组。

string line = @"DCS120170517220207-FIC-023.FLW  07-FIC-023    00060Y000000011.266525G";
string patDate = @"(?:^.{4})([2-9][0-9]{3}[0-1][0-9][0-3][0-9])";

Match m = Regex.Match(line, patDate);

foreach (Group g in m.Groups)
{
    Console.WriteLine($"{g.Index}: {g.Value}");
}

m.Value是第0组 - 整个匹配,无论分组如何。由于您明智地将第一个组标记为非捕获,因此组1是日期。

我建议命名您的捕获组,以便于维护:

string line = @"DCS120170517220207-FIC-023.FLW  07-FIC-023    00060Y000000011.266525G";
string patDate = @"(?:^.{4})(?<date>[2-9][0-9]{3}[0-1][0-9][0-3][0-9])";

Match m = Regex.Match(line, patDate);

var date = m.Groups["date"].Value;

更新

WiktorStribiżew观察到非捕获组是otiose。以下模式的行为与原始模式相同。但是,第一个捕获组仍为m.Groups[1],因为m.Groups[0]始终是整个匹配,无论是哪个组。

string patDate = @"^.{4}(?<date>[2-9][0-9]{3}[0-1][0-9][0-3][0-9])";