开发者

Parsing values from a formatted string in C#

How can I parse multiple values from a formatted string in C#?

The string is in this format: "blah blah blah (foo:this, bar:that)"

I need to parse out the foo and the bar value. The parentheses are always at the end of the line.

Edit: Sorry... that wasn't very clear. What I meant was I need to know the "foo" value and the "bar" value, so that I can say, somewhere else, "foo is this" and "bar is that".

开发者_JAVA技巧

Thanks


EDIT: updated after OP clarification.

This should do:

string input = "blah blah blah (foo:this, bar:that,1:one,2:two)";
string pattern = @"\((?:(?<Values>.*?:[^,\s]+)[,\s]*)+\)";
foreach (Match m in Regex.Matches(input, pattern))
{
    foreach (Capture c in m.Groups["Values"].Captures)
    {
        string[] values = c.Value.Split(':');
        Console.WriteLine("{0} : {1}", values[0], values[1]);
    }
}

This outputs:

  • foo : this
  • bar : that
  • 1 : one
  • 2 : two

If you need to ensure the match only occurs at the end of the string, rather than match similar formatted values elsewhere in the string, add $ to the end of the pattern:

string pattern = @"\((?:(?<Values>.*?:[^,\s]+)[,\s]*)+\)$";


Regular expressions should not be used for parsing if possible, only lexing. Pass the lexed tokens into a finite state machine for the actual parsing.


I'm making quite a few assumptions here based on your question, but this should get you headed in the right direction.

#!/usr/bin/perl

my $input = "blah blah blah (foo:this, bar:that, foo2:150)";

my @ray = ($input =~ /.*?:(\w*)/g);
foreach $x (@ray)
{
    print "Value: '$x'\n";
}

Output:

Value: 'this'
Value: 'that'
Value: '150'


As for .NET you can use captures like this:

> $s = "blah blah blah (foo:this, bar:that)"
> $result = [regex]::Match($s, '[^(]*\((?:\w+:(?<t>\w+),\s*)*\w+:(?<t>\w+)\)$')
> $result.Groups

Groups   : {blah blah blah (foo:this, bar:that), that}
Success  : True
Captures : {blah blah blah (foo:this, bar:that)}
Index    : 0
Length   : 35
Value    : blah blah blah (foo:this, bar:that)

Success  : True
Captures : {this, that}
Index    : 30
Length   : 4
Value    : that

> $result.Groups[1].captures
Index                                          Length Value
-----                                          ------ -----
20                                               4 this
30                                               4 that

it is code in PowerShell. However, PowreShell is based on .NET, so this should work in .NET.

The parsing expression is based on the example you posted, so it skips everything up to ( and then begins parsing the values. Note that (?:..) is noncapturing group so it doesn't appear in results.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜