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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:52:44+00:00 2026-06-06T14:52:44+00:00

I have a Bug at my simple REGEX. I have been trying to write

  • 0

I have a Bug at my simple REGEX.
I have been trying to write some simple regular expressions at C++ using std::regex. Here is my code so far.

#include <iostream>
#include <regex>
#include <string>

int main(void)
{
    std::string str = "Hello world";
    std::regex rx("\w+\s\w+"), rx2("ello");
    std::cout << std::boolalpha << std::regex_match(str.begin(), str.end(), rx) << "\n";
    std::cout << std::boolalpha << std::regex_search(str.begin(), str.end(), rx2) << "\n";
    return 0;
}

This program should print (according to a tutorial)

true
true

but it prints

false
false

Where am I making a mistake? Thanks in advance.

Note: I’m using g++ -std=c++0x %file.cpp% -o %file% if it helps

  • 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-06T14:52:45+00:00Added an answer on June 6, 2026 at 2:52 pm

    As already stated g++ (GCC) does not have a proper implementation for regex (it is unimplemented but still compiles).

    The Boost library has an implementation for regexes that is almost completely compatible with regexes in C++11. You could use that with minimal changes in your code (just using boost:: instead of std::).

    Here is a code that compiles and works:

    #include <iostream>
    #include <boost/regex.hpp>
    #include <string>
    
    int main(void)
    {
        std::string str = "Hello world";
        boost::regex rx("\\w+\\s\\w+"), rx2("ello");
        std::cout << std::boolalpha << boost::regex_match(str.begin(), str.end(), rx) << "\n";
        std::cout << std::boolalpha << boost::regex_search(str.begin(), str.end(), rx2) << "\n";
        return 0;
    }
    

    Note that I have also fixed missing escapes for the backslashes for rx because it does not work without it.

    To compile this you have to install libboost-regex-dev package (or something similar if not using Ubuntu/Debian) and execute this:

    g++ -std=c++0x main.cpp -lboost_regex -o test
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some simple test code as a demonstration of hooking the
I have some simple OpenGL ES code in C++ that I've run on a
I have a fairly weird 'bug' with a simple query, and I vaguely remember
Hi people, I have a question that should be simple. I am new using
I am not using flash or php - and I have been asked to
Here's a dead-simple webpage that leaks memory in IE8 using jQuery (I detect memory
I'm getting a Variable TMP_1 might not have been initialized error. Here's the snippet:
Its one of those simple questions that bug the most. I have the following
I have built a very simple CakePHP website using the Auth component and have
I have a simple console app using SQL Compact 4.0 and entity frameworks 4.

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.