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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:40:57+00:00 2026-05-12T07:40:57+00:00

I just started using Boost::regex today and am quite a novice in Regular Expressions

  • 0

I just started using Boost::regex today and am quite a novice in Regular Expressions too. I have been using “The Regulator” and Expresso to test my regex and seem satisfied with what I see there, but transferring that regex to boost, does not seem to do what I want it to do. Any pointers to help me a solution would be most welcome. As a side question are there any tools that would help me test my regex against boost.regex?

using namespace boost;
using namespace std;

vector<string> tokenizer::to_vector_int(const string s)
{
    regex re("\\d*");
    vector<string> vs;
    cmatch matches;
    if( regex_match(s.c_str(), matches, re) ) {
        MessageBox(NULL, L"Hmmm", L"", MB_OK); // it never gets here
        for( unsigned int i = 1 ; i < matches.size() ; ++i ) {
            string match(matches[i].first, matches[i].second);
            vs.push_back(match);
        }
    }
    return vs;
}

void _uttokenizer::test_to_vector_int() 
{
    vector<string> __vi = tokenizer::to_vector_int("0<br/>1");
    for( int i = 0 ; i < __vi.size() ; ++i ) INFO(__vi[i]);
    CPPUNIT_ASSERT_EQUAL(2, (int)__vi.size());//always fails
}

Update (Thanks to Dav for helping me clarify my question):
I was hoping to get a vector with 2 strings in them => “0” and “1”. I instead never get a successful regex_match() (regex_match() always returns false) so the vector is always empty.

Thanks ‘1800 INFORMATION’ for your suggestions. The to_vector_int() method now looks like this, but it goes into a never ending loop (I took the code you gave and modified it to make it compilable) and find “0”,””,””,”” and so on. It never find the “1”.

vector<string> tokenizer::to_vector_int(const string s)
{
    regex re("(\\d*)");
    vector<string> vs;

    cmatch matches;

    char * loc = const_cast<char *>(s.c_str());
    while( regex_search(loc, matches, re) ) {
        vs.push_back(string(matches[0].first, matches[0].second));
        loc = const_cast<char *>(matches.suffix().str().c_str());
    }

    return vs;
}

In all honesty I don’t think I have still understood the basics of searching for a pattern and getting the matches. Are there any tutorials with examples that explains this?

  • 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-12T07:40:57+00:00Added an answer on May 12, 2026 at 7:40 am

    The basic problem is that you are using regex_match when you should be using regex_search:

    The algorithms regex_search and
    regex_match make use of match_results
    to report what matched; the difference
    between these algorithms is that
    regex_match will only find matches
    that consume all of the input text,
    where as regex_search will search for
    a match anywhere within the text being
    matched.

    From the boost documentation. Change it to use regex_search and it will work.

    Also, it looks like you are not capturing the matches. Try changing the regex to this:

    regex re("(\\d*)");
    

    Or, maybe you need to be calling regex_search repeatedly:

    char *where = s.c_str();
    while (regex_search(s.c_str(), matches, re))
    {
      where = m.suffix().first;
    }
    

    This is since you only have one capture in your regex.

    Alternatively, change your regex, if you know the basic structure of the data:

    regex re("(\\d+).*?(\\d+)");
    

    This would match two numbers within the search string.

    Note that the regular expression \d* will match zero or more digits – this includes the empty string “” since this is exactly zero digits. I would change the expression to \d+ which will match 1 or more.

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

Sidebar

Related Questions

I have just started using Boost 1.36. These libraries would be very useful in
I have just started with using boost libraries. For one of our projects i
Just started using carrierwave with Rails and things have been going smoothly with one
I've recently started using boost. So far most things have been pretty straight forward.
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I just started using SVN, and I have a cache directory that I don't
I'm just getting started with Boost for the first time, details: I'm using Visual
Just started using Blueprint CSS and now playing with the grids but have a
Greetings. I'm just getting started with the boost::asio library and have run into some

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.