1

I have this string

hey {Bobby|Apple|Peter}, nice to meet you {David}

and this regex:

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

the answer:

Bobby
Peter
David

However, it's not getting "Apple", how can I fix this to get it as well?

Thanks!

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
unwise guy
  • 1,048
  • 8
  • 18
  • 27

2 Answers2

1

Repetition does not work for groups. Instead, try to iteratively use find on the string. Probably best to first filter out {Bobby|Apple|Peter}, get the names from that, then find {David} and get the names from that. So that would be two finds, if you keep using regular expressions. Or one find, then a split on | from the result.

Regexp for the find: \{(\w+(?:\|\w+)*)\}, then use group 1 (everything within the braces) and split the result.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • I'm not too familiar with boost::regex, but I dont think they have the filter out/find functions. – unwise guy Jul 04 '12 at 00:07
  • "filter out" whas just a human term for finding stuff and extracting the data from what you just found. Try to find some samples to iterate through matches within a string, something like [this](http://stackoverflow.com/questions/2593288/how-to-use-c-boosts-regex-iterator)? – Maarten Bodewes Jul 04 '12 at 00:19
0

Can you show the entire, minimal code that reproduces this problem? It's possible the problem is with how you're iterating through the captured groups.

hcarver
  • 7,126
  • 4
  • 41
  • 67