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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:31:54+00:00 2026-06-10T07:31:54+00:00

Tring to get C++ regex string capture to work. I have tried all four

  • 0

Tring to get C++ regex string capture to work. I have tried all four combinations of Windows vs. Linux, Boost vs. native C++ 0x11. The sample code is:

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

using namespace std;
using namespace boost;

int main(int argc, char** argv)
{
    smatch sm1;
    regex_search(string("abhelloworld.jpg"), sm1, regex("(.*)jpg"));
    cout << sm1[1] << endl;
    smatch sm2;
    regex_search(string("hell.g"), sm2, regex("(.*)g"));
    cout << sm2[1] << endl;
}

The closest that works is g++ (4.7) with Boost (1.51.0). There, the first cout outputs the expected abhelloworld. but nothing from the second cout.

g++ 4.7 with -std=gnu++11 and <regex> instead of <boost/regex.hpp> produces no output.

Visual Studio 2012 using native <regex> yields an exception regarding incompatible string iterators.

Visual Studio 2008 with Boost 1.51.0 and <boost/regex.hpp> yields an exception regarding “Standard C++ Libraries Invalid argument”.

Are these bugs in C++ regex, or am I doing something wrong?

  • 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-10T07:31:55+00:00Added an answer on June 10, 2026 at 7:31 am

    Are these bugs in C++ regex, or am I doing something wrong?

    At the time of your posting, gcc didn’t support <regex> as noted in the other answer (it does now). As for the other problems, your problem is you are passing temporary string objects. Change your code to the following:

    smatch sm1;
    string s1("abhelloworld.jpg");
    regex_search(s1, sm1, regex("(.*)jpg"));
    cout << sm1[1] << endl;
    smatch sm2;
    string s2("hell.g");
    regex_search(s2, sm2, regex("(.*)g"));
    cout << sm2[1] << endl;
    

    Your original example compiles because regex_search takes a const reference which temporary objects can bind to, however, smatch only stores iterators into your temporary object which no longer exists. The solution is to not pass temporaries.

    If you look in the C++ standard at [§ 28.11.3/5], you will find the following:

    Returns: The result of regex_search(s.begin(), s.end(), m, e, flags).

    What this means is that internally, only iterators to your passed in string are used, so if you pass in a temporary, iterators to that temporary object will be used which are invalid and the actual temporary itself is not stored.

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

Sidebar

Related Questions

I'm trying to get all words inside a string using Boost::regex in C++. Here's
I'm trying to get the following regex to work on my String: Pattern Regex
I have fooled around with regex but can't seem to get it to work.
I am trying to get the following regex to work on ios in order
I am trying to get some Regex to match and string that begins with
I'm trying to capture parts of a multi-lined string with a regex in Scala.
I'm trying to get the pipe (OR) to work. I have this regular expression
I have a regex that can match a string in multiple overlapping possible ways.
I am trying to write a regex in Java to get rid of all
I am trying get the Regex right for the following scenario but have 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.