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 8928421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:25:58+00:00 2026-06-15T08:25:58+00:00

I’m using boost::regex to parse some formatting string where ‘%’ symbol is escape character.

  • 0

I’m using boost::regex to parse some formatting string where ‘%’ symbol is escape character. Because I do not have much experience with boost::regex, and with regex at all to be honest I do some trial and error. This code is some kind of prototype that I came up with.

std::string regex_string = 
            "(?:%d\\{(.*)\\})|"                   //this group will catch string for formatting time
            "(?:%([hHmMsSqQtTlLcCxXmMnNpP]))|"    //symbols that have some meaning
            "(?:\\{(.*?)\\})|"                    //some other groups
            "(?:%(.*?)\\s)|"
            "(?:([^%]*))";

    boost::regex regex;
    boost::smatch match;

    try
    {
        regex.assign(regex_string, boost::regex_constants::icase);
        boost::sregex_iterator res(pattern.begin(), pattern.end(), regex);
        //pattern in line above is string which I'm parsing
        boost::sregex_iterator end;
        for(; res != end; ++res)
        {
            match = *res;
            output << match.get_last_closed_paren();
            //I want to know if the thing that was just written to output is from group describing time string
            output << "\n";
        }


    }
    catch(boost::regex_error &e)
    {
        output<<"regex error\n";
    }

And this works pretty good, on the output I have exactly what I want to catch. But I do not know from which group it is. I could do something like match[index_of_time_group]!="" but this is kind of fragile, and doesn’t look too good. If I change regex_string index that was pointing on group catching string for formatting time could also change.

Is there a neat way to do this? Something like naming groups? I’ll be grateful for any help.

  • 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-15T08:25:59+00:00Added an answer on June 15, 2026 at 8:25 am

    You can use boost::sub_match::matched bool member:

    if(match[index_of_time_group].matched) process_it(match);
    

    It is also possible to use named groups in regexp like: (?<name_of_group>.*), and with this above line could be changed to:

    if(match["name_of_group"].matched) process_it(match);
    
    • 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 have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

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.