2

I have this code snippet that I derived from something that I found online:

std::regex pieces_regex("([a-z]+)\\.([a-z]+)", std::regex_constants::basic);
std::smatch pieces_match;
std::string fname = "foo.txt";
cout << " fname " << fname << endl;
if (std::regex_match(fname, pieces_match, pieces_regex)) {
    std::cout << fname << '\n';
    for (size_t i = 0; i < pieces_match.size(); ++i) {
        std::ssub_match sub_match = pieces_match[i];
        std::string piece = sub_match.str();
        std::cout << "  submatch " << i << ": " << piece << '\n';
    }   
} 

the pieces_regex constructor didn't work with my compiler, so I had to change the line from

std::regex pieces_regex("([a-z]+)\\.([a-z]+)");

to

std::regex pieces_regex("([a-z]+)\\.([a-z]+)", std::regex_constants::basic);

My regex_match line doesn't return anything now, and the size of pieces_match is zero. I tried adding several different flags to regex_match, like basic or match_any, but it didn't help. The original code can be found here: http://en.cppreference.com/w/cpp/regex/regex_match

Anyone have an idea what I'm missing?

Thanks!

n00b programmer
  • 2,671
  • 7
  • 41
  • 56

1 Answers1

0

Standard regex (no second 'flags' argument) uses ECMA script as default. Try this instead:

std::regex pieces_regex("([a-z]+)\\.([a-z]+)", std::regex_constants::ECMAScript);
//                                                                   ^

And you should see some results...

EDIT: As you have trouble with ECMA script: You chose the grammar unluckily: extended, awk and egrep all would swallow your expression as expected...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59