Match parens recursively in boost::regex?

Posted on 2012-08-26
Medium Priority
Last Modified: 2012-08-26
Does Boost regex support recursive matching--for example to find matched parens as the PHP pattern below does?

\( ( (?>[^()]+) | (?R) )* \)
Question by:anAppBuilder
LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 38335093
Yes, it should work--so long as you double-up the backslashes for C++ escaping.  https://svn.boost.org/trac/boost/wiki/soc/2007/RegexRecursiveMatch#Recursivematches

e.g.  (Boost 1.42)

#include <boost/regex.hpp>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
    boost::regex expr("\\(((?>[^()]+)|(?R))*\\)");
    boost::cmatch what;
    if (argc < 2) return -1;
    if (boost::regex_match(argv[1], what, expr))
        cout << "Matched!\n\n";
       cout << "Did not match!\n\n";
    return 0;

Open in new window


Author Comment

ID: 38335179
Works great.  Thank you, kaufmed.

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question