3

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".

Thanks

1
  • @Ross check my updated solution Commented Feb 3, 2010 at 19:04

4 Answers 4

1

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]*)+\)$";
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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'

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.