0

I am trying to split a string, in the following format:

9A ##{Indie; Rock}##

(The string comes from an mp3 tag via TagLib)

The code is:

        string[] parts = Regex.Split(comment,"##{");
        string prefix = parts[0];
        Console.WriteLine(parts[1]);
        string[] parts2 = Regex.Split(parts[1], "}##");
        string keywords = parts2[0];
        string suffix = parts2[1];

However, at the console.writeline, I'm getting back:

Indie

Whereas I'd expect:

Indie; Rock}##

I assume it's something today with the semi-colon terminating the line early, but I don't know why (or how to fix it).

Ben
  • 4,281
  • 8
  • 62
  • 103
  • 1
    Actually, when I run the first 3 lines of your code with `var comment="9A ##{Indie; Rock}##";`, I get the expected output of `Indie; Rock}##`. Voting to close. – spender Apr 28 '13 at 17:43
  • me too. tried the above code `parts[1]` returns the expected value. Converted to VB also returns the same expected value. – ajakblackgoat Apr 28 '13 at 17:48
  • Actually - you're right. It turns out that the problem was in pulling the comment via TagLib. It was truncating if there was a semicolon there. – Ben Apr 30 '13 at 08:23

1 Answers1

2

Try using capture groups. http://www.regular-expressions.info/named.html

This regex worked for me

##{(?<first>.*);(?<second>.*)}##

Also, Expresso can be very useful for regex problems http://www.ultrapico.com/ExpressoDownload.htm

Jras
  • 518
  • 3
  • 12
  • Can you do this even if you don't know how many items you will need to capture? – Ben Apr 29 '13 at 07:01
  • Yes, You would use Regex.Match() and then iterate over the Groups property. Here is an example http://stackoverflow.com/questions/5767605/looping-through-regex-matches and a fairly decent tutorial http://www.dotnetperls.com/regex-match. – Jras Apr 30 '13 at 02:23