unwise guy
unwise guy

Reputation: 1128

Regular Expressions in C++ using Boost

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

Upvotes: 1

Views: 220

Answers (2)

tomasz
tomasz

Reputation: 13072

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.

Upvotes: 2

Arpegius
Arpegius

Reputation: 5887

If only 0 or 2 vertical bars:

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

For 0 or more:

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

Upvotes: 1

Related Questions