1

I try to do the most basic regex example in C++ using the default lib, and I keep getting either crashes or incoherent behavior.

// with -std=c++11
#include <regex>
using namespace std;

int main()
{
    // Copied from the documentation, this one works
    if (std::regex_match ("subject", std::regex("(sub)(.*)") ))
        std::cout << "string matched\n";
    // The most simple example I could try, crash with "regex_error"
    if (std::regex_match ("99", std::regex("[0-9]*") ))
        std::cout << "integer matched\n";
}

I've tried multiple syntaxes and flags, but nothing seems to work. Since my code seems to be matching all the examples I can find, I'm struggling to see what I'm missing.

5
  • 3
    See Is gcc 4.8 or earlier buggy about regular expressions. Commented May 16, 2016 at 10:35
  • Hum, so how am I supposed to actually use regex in c++ ? Commented May 16, 2016 at 10:46
  • @Dillinur gcc is up to 6.1 now. You could try upgrading your compiler. Commented May 16, 2016 at 10:46
  • @Dillinur: At least do not use regex patterns that match empty strings. Why do you need an empty (null) string? Just use [0-9]+. Commented May 16, 2016 at 10:49
  • I was just trying all the possible regex, any of the '*' '+' chars were just crashing the regex constructor.. Commented May 16, 2016 at 11:21

1 Answer 1

1

As @Wiktor Stribiżew stated, it was just my compiler being too old. Updating the compiler (from gcc 4.1 to gcc 4.9) solved the problem!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.