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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

that regex doesn't seem to be valid... the error I received was "is not a valid regular expression: "Invalid content of repeat range."
@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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.