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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:39:08+00:00 2026-05-18T01:39:08+00:00

I’m writing a small command-line program that asks the user for polynomials in the

  • 0

I’m writing a small command-line program that asks the user for polynomials in the form ax^2+bx^1+cx^0. I’m going to parse the data later but for now I’m just trying to see if I can match the polynomial with the regular expression(\+|-|^)(\d*)x\^([0-9*]*)My problem is, it doesn’t match multiple terms in the user-entered polynomial unless I change it to((\+|-|^)(\d*)x\^([0-9*]*))*(the difference is the entire expression is grouped and has an asterisk at the end). The first expression works if I type something such as “4x^2” but not “4x^2+3x^1+2x^0“, since it doesn’t check multiple times.

My question is, why won’t Boost.Regex’sregex_match()find multiple matches within the same string? It does in the regular expression editor I used (Expresso) but not in the actual C++ code. Is it supposed to be like that?

Let me know if something doesn’t make sense and I’ll try to clarify. Thanks for the help.

Edit1: Here’s my code (I’m following the tutorial here: http://onlamp.com/pub/a/onlamp/2006/04/06/boostregex.html?page=3)

int main()
{
    string polynomial;

    cmatch matches; // matches

    regex re("((\\+|-|^)(\\d*)x\\^([0-9*]*))*");

    cout << "Please enter your polynomials in the form ax^2+bx^1+cx^0." << endl;

    cout << "Polynomial:";
    getline(cin, polynomial);

    if(regex_match(polynomial.c_str(), matches, re))
    {
        for(int i = 0; i < matches.size(); i++)
        {
            string match(matches[i].first, matches[i].second);
            cout << "\tmatches[" << i << "] = " << match << endl;
        }
    }

    system("PAUSE");
    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-18T01:39:09+00:00Added an answer on May 18, 2026 at 1:39 am

    You’re using the wrong thing — regex_match is intended to check whether a (single) regex matches the entirety of a sequence of characters. As such, you need to either specify a regex that matches the whole input, or use something else. For your situation, it probably makes the most sense to just modify the regex as you’ve already done (group it and add a Kleene star). If you wanted to iterate over the individual terms of the polynomial, you’d probably want to use something like a regex_token_iterator.

    Edit: Of course, since you’re embedding this into C++, you also have to double all your backslashes. Looking at it, I’m also a little confused about the regex you’re using — it doesn’t look to me like it should really work quite right. Just for example, it seems to require a “+”, “-” or “^” at the beginning of a term, but the first term won’t normally have that. I’m also somewhat uncertain why there would be a “^” at the beginning of a term. Since the exponent is normally omitted when it’s zero, it’s probably better to allow it to be omitted. Taking those into account, I get something like: “[-+]?(\d*)x(\^([0-9])*)”.

    Incorporating that into some code, we can get something like this:

    #include <iterator>
    #include <regex>
    #include <string>
    #include <iostream>
    
    int main() { 
    
        std::string poly = "4x^2+3x^1+2x";
    
        std::tr1::regex term("[-+]?(\\d*)x(\\^[0-9])*");
    
        std::copy(std::tr1::sregex_token_iterator(poly.begin(), poly.end(), term),
            std::tr1::sregex_token_iterator(), 
            std::ostream_iterator<std::string>(std::cout, "\n"));
        return 0;
    }
    

    At least for me, that prints out each term individually:

    4x^2
    +3x^1
    +2x

    Note that for the moment, I’ve just printed out each complete term, and modified your input to show off the ability to recognize a term that doesn’t include a power (explicitly, anyway).

    Edit: to collect the results into a vector instead of sending them to std::cout, you’d do something like this:

    #include <iterator>
    #include <regex>
    #include <string>
    #include <iostream>
    
    int main() {   
        std::string poly = "4x^2+3x^1+2x";
    
        std::tr1::regex term("[-+]?(\\d*)x(\\^[0-9])*");
        std::vector<std::string> terms;
    
        std::copy(std::tr1::sregex_token_iterator(poly.begin(), poly.end(), term),
            std::tr1::sregex_token_iterator(), 
            std::back_inserter(terms));
    
        // Now terms[0] is the first term, terms[1] the second, and so on.
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.