tags:

views:

18

answers:

1

Hi, another one stuck in Regex land... having the following line

Word "Blabla" -Option1:Bla di Bla -Option2:Ha

I want to match the 2 "options" at the end such that I have the groups:

  • Option1
  • Bla di Bla
  • Option2
  • Ha

What I have so far is this: -(\w+?):(.+?)(?=-|$)

Which I thought should mean: "A hyphen, followed by a word, followed by a colon, followed by some amount of characters that are followed either by a hyphen or a newline, without consuming any of the two"

But somehow it goes wrong, only the first 2 groups will be captured. Where am I going wrong?

+1  A: 

That is correct. You didn't mention what language you're using, but here's how it works in PHP:

$text = 'Word "Blabla" -Option1:Bla di Bla -Option2:Ha';
preg_match_all('/-(\w+?):(.+?)(?=-|$)/', $text, $matches, PREG_SET_ORDER);
print_r($matches);

which produces:

Array
(
    [0] => Array
        (
            [0] => -Option1:Bla di Bla 
            [1] => Option1
            [2] => Bla di Bla 
        )

    [1] => Array
        (
            [0] => -Option2:Ha
            [1] => Option2
            [2] => Ha
        )

)
Bart Kiers
yes...looks like I messed it up in a silly fashion. Since we all agree, we can close this...
flq