简体   繁体   中英

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

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.

If only 0 or 2 vertical bars:

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

For 0 or more:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM