开发者

Why is my .NET regex not working correctly?

I have a text file which is in the format:

key1:val1,
key2:val2,
key3:val3

and I am trying to parse the key/value pairs out with a regex. Here is the regex code I am using with the same example:

string input = @"key1:val1,
key2:val2,
key3:val3";

var r = new Regex(@"^(?<name>\w+):(?<value>\w+),?$", RegexOptions.Multiline | RegexOptions.ExplicitCapture);

foreach (Match m in r.Matches(input))
{
    Console.WriteLine(m.Groups["name"].Value);
    Console.WriteLine(m.Groups["value"].Value);
}

When I loop through r.Matches, sometimes certain k开发者_如何学JAVAey/value pairs don't appear, and it seems to be the ones with the comma at the end of the line - but I should be taking that into account with the ,?. What am I missing here?


this might be a good situation for String.Split rather than a regex:

foreach(string pair in input.Split(new Char [] {','}))
{
   string [] items = pair.Split(new Char [] {':'});
   Console.WriteLine(items[0]);
   Console.WriteLine(items[1]);
}


The problem is that your regular expression is not matching the newline in the first two lines.

Try changing it to

@"^(?<name>\w+):(?<value>\w+),?(\n|\r|\r\n)?$"

and it should work.
By the way, I love regular expressions, but given the problem you are trying to solve, go for the string.Split solution. It will be much easier to read...

EDIT: after reading your comment, where you say that this is a simplified version of your problem, then maybe you could simplify the expression by adding some "tolerance" for spaces / newline at the end of the match with

@"^(?<name>\w+):(?<value>\w+),?\s*$"

Also, when you play with regular expressions, test them with a tool like Expresso, it saves a lot of time.


Get rid of the RegexOptions.Multiline option.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜