繁体   English   中英

无法构建正确的正则表达式

[英]Trouble constructing correct regular expression

试图构建一个正则表达式来匹配下面列表的每一行。

6220    1   10  Because he's the hero Gotham deserves,
6220    9   10   
5181    5   7   in time, like tears in rain. Time to die.
6220    3   10  So we'll hunt him.
6220    5   10  Because he's not a hero.
5181    6   7    
5181    2   7   shoulder of Orion. I watched C-beams
5181    4   7   Gate. All those moments will be lost
6220    6   10  He's a silent guardian.
5181    3   7   glitter in the dark near the Tannhäuser
6220    7   10  A watchful protector.
5181    1   7   believe. Attack ships on fire off the
6220    0   10  We have to chase him.
5181    0   7   I've seen things you people wouldn't
6220    4   10  Because he can take it.
6220    2   10  but not the one it needs right now.
6220    8   10  A Dark Knight.

一切都很好,直到一行有一个空白的“消息”,例如第 2 行和第 6 行。这是当前正则表达式的样子。

var regex = new Regex(
    @"(?<id>\d+)\s+(?<index>\d+)\s+(?<count>\d+)\s+(?<message>.+)?");

foreach (Match match in regex.Matches(File.ReadAllText("samples.txt")))
{
    Console.WriteLine(match.Groups["message"].Value);
}

如果原始消息组为空,当前正则表达式会将自身下方的行设置为消息。 我已经尝试了很多事情,以及使消息可选。 如何让正则表达式将消息识别为空?

(?<id>\d+)\s+(?<index>\d+)\s+(?<count>\d+)\s(?<message>.+)?

使您的\\s+成为\\s这样就可以匹配一些东西而不是吃掉它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM