1

I need to parse the following string

/MyData.csv(MemberCount.sum,Production.sum[Salesperson="James Almond","Area="Europe "Area 1" (Germany)",Area="North America",Area="North America [Level A]"])

The first part is easy, however the clause within the brackets ([]) is giving me a bit of headache since it can contain double quotes as the example shows. (note the content within the brackets can change dynamically)

I expect the following output when parsing the last part of the string :

Salesperson James Almond Area Europe "Area 1" (Germany) Area North America Area North America [Level A]

I've been working with regex but can't seem to get it right. Hoping someone have the magic!

2
  • 1
    what if you simply did String.Split() on "Area=" and did a little post manipulation? Commented Aug 26, 2013 at 12:28
  • I realize that my example should have had another example, Area could be substituted with another label as well like "CountryName" (it depends on the columns being queried) Commented Aug 26, 2013 at 12:36

2 Answers 2

1

You can use the fact that the 'true' closing double quotes are either followed by a comma or the closing square bracket:

Area="(?<Area>.*?)"(?=\]|,)

regex101 demo.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jerry. regex101 demo is very useful. I edited the question a bit as I didn't realize I made the data a bit too uniform.
@ptrdk You're welcome! Also, is the part within the square brackets you need the only part with the format word = "word"? If so, you can use this: (\w+)="(.*?)"(?=\]|,) (updated regex101). If that's that, I'll update my answer with it.
0

You may give a try to the Balancing Group Definitions:

string pattern = @"^[^\[\]]*" +
                @"(" +
                @"((?'Open'\[)[^\[\]]*)+" +
                @"((?'Close-Open'\])[^\[\]]*)+" +
                @")*" +
                @"(?(Open)(?!))$";

var results =
    Regex.Match(input, pattern)
    .Groups["Close"].Value
    .Split(new char[] { '=', ',' });

This outputs:

Salesperson
"James Almond"
Area
"Europe "Area 1" (Germany)"
Area
"North America"
Area
"North America [Level A]"

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.