1

So far.. I have this test string:

Hello {John|Paul|Cindy}, hows {david}?

and my expression is:

(\{\w+\})

However, it only returns david. I want to be able to grab John, Paul, and Cindy. There would only be 0 or 2 vertical bars. any ideas?

Thanks

unwise guy
  • 1,048
  • 8
  • 18
  • 27
  • \w matches a normal charakter. But | is none. Therefore it matches only {david}. {John|Paul|Cindy} is not matched because it contains symbols | that are not contained in \w. But I am not sure what you want to achive? – Haatschii Jul 03 '12 at 23:21
  • yes I see that, I want to just grab all of the names. – unwise guy Jul 03 '12 at 23:23

2 Answers2

2

If it's not some kind of competition, I would simply use two regular expressions:

{[\w|]+} to grab each pair of curly brackets along with its content, then, on each result, \w+ to extract words.

You can't go simpler using just one regex.

tomasz
  • 12,574
  • 4
  • 43
  • 54
1

If only 0 or 2 vertical bars:

(\{\w+\}|\{\w+\|\w+\|\w+\})

For 0 or more:

(\{\w+(\|\w+)*\})
Arpegius
  • 5,817
  • 38
  • 53