1

example string:

std::string sentence = "Hello {Bobby}, hows {Johns}?."

I want to be able to grab everything inside the curly braces using boost::regex, any help or guidance would be appreciated.

the string can contain {bobby|john|cindy} or {bobby||cindy} or {{bobby}} in which it'll be {bobby}. I'm trying to figure out the regex for that.

Thanks.

unwise guy
  • 1,048
  • 8
  • 18
  • 27

2 Answers2

2

The overall approach and code example is described here.

The regex you need seems to be:

([^{}]*\{([^{}]*)\})+

This regex will not match at all unless the string uses correctly paired, non-nested braces. If it matches, you can use regex_iterator to process every second subgroups in each match.

Community
  • 1
  • 1
Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75
  • that regex doesn't seem to be valid... the error I received was "is not a valid regular expression: "Invalid content of repeat range." – unwise guy Jul 03 '12 at 21:49
  • @unwiseguy - I forgot to escape braces, fixed. No need to escape them in brackets. Braces outside of brackets denote repeat ranges such as a{3} = aaa – Jirka Hanika Jul 03 '12 at 21:52
0

Based upon your comment on David's answer, you may be into territory that a regex can't parse directly. Nested stuff like you described with the double-braces strikes me as something that you may need an actual grammar to parse. In which case you get the "joys" of learning about Bison. If it can't parse it, you need something way more specific than is easy.

Single curly-brace, fairly simple. What's inside the double... thar be dragons. Because then you get into things like this: "What {do you {want|here}}?" A grammar provided to Bison can work that out. I'm not sure a simple regex can.

Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54