Match parens recursively in boost::regex?

Posted on 2012-08-26
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 74

    Accepted Solution

    Yes, it should work--so long as you double-up the backslashes for C++ escaping.

    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

    Works great.  Thank you, kaufmed.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now