0

I have the following regex

\b(?!^;#)\w+\s*\w+\|\b

and this sample string.

-1;#Class Study|0b4dac95-9e17-4af9-b849-6d283a99b561;#-1;#Matrix|dda77641-7b25-40f9-bb65-a0bca13776d3";

I need to just match the label which comes after the ;#

       string multipleFieldValue = "-1;#Class Study|0b4dac95-9e17-4af9-b849-6d283a99b561;#-1;#Matrix|dda77641-7b25-40f9-bb65-a0bca13776d3";
        var regex = new Regex(@"\b(?!^;#)\w+\s*\w+\|\b");
        string[] labels = multipleFieldValue.Split(new[] { ";#" },StringSplitOptions.None );
        var matches = regex.Matches(multipleFieldValue);


        Assert.AreEqual(2, matches.Count);

currently this returns the label but also returns the | I want to eliminate the | as well

monkeyjumps
  • 702
  • 1
  • 14
  • 24
  • This part `(?!^;#)` will never match (in a negative way) when a `\w` comes after it. –  Apr 07 '16 at 21:59
  • I'd throw away the word boundares and just go with `(?<=;#)[^|]*(?=\|$)` –  Apr 07 '16 at 22:04
  • For `(?!^;#)`, neither `;` nor `#` are a word `\w`, so it will never be matched (in a negative way) when the next character should be a word `\w`. So, it is basically not being used. –  Apr 07 '16 at 22:08
  • And if you're just looking to find the labels, no need for split. –  Apr 07 '16 at 22:12
  • (?<=;#)[^|]*(?=\|$) returns zero matches – monkeyjumps Apr 07 '16 at 22:35
  • `(?<=;#)[^|]*(?=\||$)` returns zero matches? How about `(?<=;#)[^|]*` ? –  Apr 07 '16 at 22:42
  • returns incorrect values. {Class Study,-1#Matrix} – monkeyjumps Apr 07 '16 at 22:53
  • It returns anything between `;#` and `|`. That's the two delimiters you set. If it's not correct, then the form is not correct the way you set up the regex. –  Apr 07 '16 at 23:17

1 Answers1

2

Just wrap the | and word boundary in a lookahead

\b(?!^;#)\w+\s*\w+(?=[?^(|)]\b)

Demo here

James Buck
  • 1,640
  • 9
  • 13
  • what if i need the label | termId returned? ie {Class Study|0b4dac95-9e17-4af9-b849-6d283a99b561 , Matrix|dda77641-7b25-40f9-bb65-a0bca13776d3} – monkeyjumps Apr 07 '16 at 23:13