i’m trying to write regular expressions in c++ but i keep getting compilation errors. i looked over the web but couldn’t find an answer to that.
i’m copy pasted an example such as:
#include <iostream>
#include <tr1/regex> //<regex> didn't compile
#include <string>
using namespace std;
int main(int argc, char **argv) {
string str = "127.0.0.1";
std::tr1::regex rx("127.0.0.1");
regex_match(str.begin(), str.end(), rx);
return 0;
}
i’m getting /tmp/cc0zldN2.o: In function std::tr1::basic_regex<char, std::tr1::regex_traits<char> >::basic_regex(char const*, unsigned int)':.... undefined reference to std::tr1::basic_regex<char, std::tr1::regex_traits<char> >::_M_compile()...
from what i see this syntax is used in windows. i’m using linux with a GNU project C and C++ compiler.
anyone have references to regex examples that match my compiler?
thanks!
Last time I checked (about half a year ago), GCC didn’t have the actual TR1 implementation. For whatever reason they added the headers, but skipped the implementation (instead of adding some sort of warnings or errors (or simply not deploying the headers)).
If you don’t have to use GCC, use MSVC, they’ve got a complete implementation, or use an alternative library providing regular expressions (PCRE, the GNU one (don’t remember it’s name), SLRE, etc.).