Hi,
I'm currently playing around a bit with the new <regex> library introduced in C++11 and having some problems with the ECMAScript syntax which is used to specify regular expression patterns.
I've written a small program that allows me to specify a pattern with an expression on the command line and then tries to match the expression with the pattern at runtime. (I'll post the code below but I don't think that the problem lies there)
So, when I invoke something like that everything just works as expected:
Code:
./a.out '([^,]+),?' abc,
pattern matched!
submatch: <abc,>
submatch: <abc>
The pattern matches one or more occurrences of any character that isn't a comma, and optionally a comma at the end of the string.
But when I try to match a string like abc,def,ghi with a slightly modified pattern something goes wrong:
Code:
./a.out '(([^,]+),?)+' abc,def,ghi
pattern matched!
submatch: <abc,def,ghi>
submatch: <ghi>
submatch: <ghi>
The output expected from me would be
Code:
pattern matched!
submatch: <abc,def,ghi>
submatch: <abc>
submatch: <def>
submatch: <ghi>
So why does the regular expression machine what it does, and how can I get it working? It would make me very happy if someone could give me a hint!
The code of my program is shown below; but as already mentioned, I don't think the problem can be found there.
Code:
#include <iostream>
#include <regex>
#include <vector>
int main(int argument_count, char** argument_vector)
{
std::vector<char*> arguments{argument_vector,
argument_vector+argument_count};
if (arguments.size()<3) {
std::cerr << "invalid number of arguments" << std::endl;
return 1;
}
std::regex expression{arguments.at(1)};
std::smatch match;
if (std::regex_match(std::string{arguments.at(2)}, match, expression)) {
std::cout << "pattern matched!" << std::endl << std::endl;
for (const auto& submatch:match) {
std::cout << "submatch: <" << submatch << ">" << std::endl;
}
} else {
std::cout << "pattern didn't match!" << std::endl;
}
return 0;
}