c++11 - std::regex fails with g++? -


i'm trying use c++11's regex simple filtering task, couldn't make work want it. started write separate demonstration program.

the thing simplest things fail miserably. example:

#include <regex> #include <string> #include <iostream>  int main() {   std::vector<std::string> inputs;   inputs.push_back("1");   inputs.push_back("123");   inputs.push_back("a");   inputs.push_back("apple");   inputs.push_back(":apple3.worm");    std::string pattern("[0-9]");   std::regex r(pattern, std::regex_constants::grep);    for(auto const &s: inputs)   {     bool ok = std::regex_match(s, r);     std::cout << (ok?"pos":"neg") << ": " << s << std::endl;   }   return 0; } 

compiled without warnings g++ -wextra -pedantic -std=c++11 -o3 rfail.cpp -o rfail. output:

pos: 1 neg: 123 pos: neg: apple neg: :apple3.worm 

same happend when replace [0-9] [[:digit:]]. happening? do wrong?

update:

$ g++ -v using built-in specs. collect_gcc=g++ collect_lto_wrapper=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper target: x86_64-linux-gnu configured with: ../src/configure -v --with-pkgversion='ubuntu 4.8.4-2ubuntu1~14.04.3' --with-bugurl=file:///usr/share/doc/gcc-4.8/readme.bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu thread model: posix gcc version 4.8.4 (ubuntu 4.8.4-2ubuntu1~14.04.3) 

if read regex_match doc carefuly, you'll notice that:

the entire target sequence must match regular expression function return true (i.e., without additional characters before or after match). function returns true when match part of sequence, see regex_search.

thus, if want check if string contains @ least 1 number, change regex .*[0-9].*


note can't reproduce output, mine is:

pos: 1 neg: 123 neg: // <- here's diff neg: apple neg: :apple3.worm 

(compiled apple llvm version 7.3.0 (clang-703.0.29))


given version of gcc, seems it's running highly experimental implementation of <regex> has been included in gcc 4.9 more information bug here.

you should consider update if consider using regex within code.