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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:28:51+00:00 2026-05-13T13:28:51+00:00

I have some difficulties in understanding if-then-else conditionals in regular expressions. After reading If-Then-Else

  • 0

I have some difficulties in understanding if-then-else conditionals in regular expressions.

After reading If-Then-Else Conditionals in Regular Expressions I decided to write a simple test. I use C++, Boost 1.38 Regex and MS VC 8.0.

I have written this program:

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

int main()
{
    std::string str_to_modify = "123";
    //std::string str_to_modify = "ttt";
    boost::regex regex_to_search ("(\\d\\d\\d)");
    std::string regex_format ("(?($1)$1|000)");

    std::string modified_str = 
         boost::regex_replace(
           str_to_modify, 
           regex_to_search, 
           regex_format, 
           boost::match_default | boost::format_all | format_no_copy );

    std::cout << modified_str << std::endl;

    return 0;

}

I expected to get “123” if str_to_modify has “123” and to get “000” if I str_to_modify has “ttt”. However I get ?123123|000 in the first case and nothing in second one.

Coluld you tell me, please, what is wrong with my test?

The second example that still doesn’t work :

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

int main()
{
    //std::string str_to_modify = "123";
    std::string str_to_modify = "ttt";
    boost::regex regex_to_search ("(\\d\\d\\d)");
    std::string regex_format ("(?1foo:bar");

    std::string modified_str = 
        boost::regex_replace(str_to_modify, regex_to_search, regex_format, 
            boost::match_default | boost::format_all | boost::format_no_copy );

    std::cout << modified_str << std::endl;

    return 0;

}
  • 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-05-13T13:28:52+00:00Added an answer on May 13, 2026 at 1:28 pm

    I think the format string should be (?1$1:000) as described in the Boost.Regex docs.

    Edit: I don’t think regex_replace can do what you want. Why don’t you try the following instead? regex_match will tell you whether the match succeeded (or you can use match[i].matched to check whether the i-th tagged sub-expression matched). You can format the match using the match.format member function.

    #include <iostream>
    #include <string>
    #include <boost/regex.hpp>
    
    int main()
    {
        boost::regex regex_to_search ("(\\d\\d\\d)");
    
        std::string str_to_modify;
        while (std::getline(std::cin, str_to_modify))
        {
            boost::smatch match;
            if (boost::regex_match(str_to_modify, match, regex_to_search))
                std::cout << match.format("foo:$1") << std::endl;
            else
                std::cout << "error" << std::endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some difficulties with understanding BindingSource's behaviour. Let's look at following example: Creating
I am having difficulties understanding AsyncTask, even after reading everything about it on Developer.Android.
I have some difficulties understanding how the access method Do not move cursor automatically
I have some difficulties understanding how Boost.MultiIndex is implemented. Lets say I have the
I have some difficulties in understanding a function Win32_AllocatedResource() and its fields antecedent and
I'm working with gcc 4.4.5, and have some difficulties in understanding the right shift
I am new to WCF and I have some difficulties understanding certain things... I
i have some understanding difficulties. i have some checkboxes in html like this: <input
I have some difficulties in my project. I got the RGB values from a
I have some difficulties for using Ruby block, passing in a method. As in

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.