Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8237355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:26:48+00:00 2026-06-07T19:26:48+00:00

I’m using boost::regex_match and I’m trying to find a function to obtain the position

  • 0

I’m using boost::regex_match and I’m trying to find a function to obtain the position where the regular expression stopped matching. I couldn’t find any attribute in the boost::match_results object, but several code snippets in which boost::regex_search is used to show the submatches. Am I on the right path with my implementation or do I’ve to do something different in order to achieve that? My code looks as follows:

int main(int args, char** argv)
{
    boost::match_results<std::string::const_iterator> what;

    if(args == 3) 
    {
        std::string text(argv[1]);
        boost::regex expression(argv[2]);

        std::cout << "Text : " << text << std::endl;
        std::cout << "Regex: " << expression << std::endl;

        if(boost::regex_match(text, what, expression, boost::match_default) != 0) 
        {
            int i = 0;

            for(boost::match_results<std::string::const_iterator>::const_iterator it=what.begin(); it!=what.end(); ++it) 
            {
                std::cout << "[" << (i++) << "] " << it->str() << std::endl;
            }
            std::cout << "Matched!" << std::endl;
        } 
        else 
        {
            std::string::const_iterator start = text.begin();
            std::string::const_iterator end   = text.end();

            while(boost::regex_search(start, end, what, expression)) 
            {
                std::string submatch(what[1].first, what[1].second);
                std::cout << submatch << std::endl;
                start = what[0].second;
            }
            std::cout << "Didn't match!" << std::endl;
        }
    } //if(args == 3)
    else 
    {
        std::cout << "Invalid usage! $> ./boost-regex <text> <regex>" << std::endl;
    }
    return 0;
}

Output:

$> ./boost_regex "We're building it up to burn it down" ".*(build.*)(to.*)(burn.*)"
Text : We're building it up to burn it down
Regex: .*(build.*)(to.*)(burn.*)
[0] We're building it up to burn it down
[1] building it up
[2] to
[3] burn it down
Matched!

$> ./boost_regex "We're building it up to burm it down" ".*(build.*)(to.*)(burn.*)"
Text : We're building it up to burm it down
Regex: .*(build.*)(to.*)(burn.*)
Didn't match!

For the last input I’d like to have the something similar to:

Text : We're building it up to burm it down
Regex: .*(build.*)(to.*)(burn.*)
[0] We're building it up to
[1] building it up
[2] to
Didn't match!

Thanks in advance …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:26:50+00:00Added an answer on June 7, 2026 at 7:26 pm

    To start with, you have a problem with the regex in your example. Because it can’t match the (burn.*) subgroup, the entire regex fails to match anything, and returns no results. Adding a “?” after (burn.*) would make it so the first two subgroups would match, and the third would not.

    I would checkout rubular.com, it’s a great tool for tweaking your regular expressions and watching them work in realtime.

    To test if a subgroup did or did not participate in a match, you need to check the sub_match::matched boolean variable in your for loop, it will be true if the subgroup matched. check the documentation for boost::submatch here

    Hope this helped, it’s actually my first Stackoverflow post, answer, or comment. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.