2013-02-27 60 views
-2
const string input = "===== Action: [Action1] State: [State1] by:[user1] at [timestamp1] ===== [Notes1]===== Action: [Action2] State: [State2] by:[user2] at [timestamp2] ===== [Notes2]"; 

Regex expression = new Regex(@"===== Action: (?<Action>.*?)State:(?<State>.*?)by:(?<userid>.*?)at(?<timestamp>.*?)=====(?<Note>.*?)"); 

var results = expression.Matches(input); 
foreach (Match match in results) 
{ 
    Console.WriteLine("UserId:" + match.Groups["userid"].Value); 
    Console.WriteLine("Action:" + match.Groups["Action"].Value); 
    Console.WriteLine("State:" + match.Groups["State"].Value); 
    Console.WriteLine("TimeStamp:" + match.Groups["timestamp"].Value); 
    Console.WriteLine("Note:" + match.Groups["Note"].Value); 
} 

我能够正确解析除Note之外的所有值。任何想法我做错了什么?如何匹配行尾的句子?

+1

我编辑了自己的冠军。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 – 2013-02-27 16:50:47

+0

我们能否定义您的需求和/或预期产出与实际产出? – 2013-02-27 16:52:23

回答

1

你忘了使用(?======|$)regex .. (?<Note>.*?)在你的正则表达式的结尾会匹配一切,直到最终这是不是你想要的..

所以正则表达式将

===== Action: (?<Action>.*?)State:(?<State>.*?)by:(?<userid>.*?)at(?<timestamp>.*?)=====(?<Note>.*?)(?======|$) 

如果输入被换行符分隔,使用SinglelineMode选项

+0

谢谢,这工作! – user1889978 2013-02-27 18:37:20

1

我的心理调试感觉告诉我,在你的正则表达式的末尾.*?不贪心,a nd只会选择0个空格(满足.*?)。

我想你可能要在某些符号上班到两个你的输入和你的正则表达式来确定,其中Note的到底应该

+0

我不认为它会返回任何字符(因为'。*?'应该匹配“尽可能少的任意数量的字符,包括0”) – Nuffin 2013-02-27 17:01:24

+0

@Nuffin固定.. – 2013-02-27 17:02:14