2017-04-18 74 views
7

我指出,正则表达式,含有两个图案与OR条件,如果第一图案是第二图案的开始部分不匹配的样本串(在铛3.5和铛3.8测试):是铿锵c + + 11功能或错误std :: regex_match?

std::regex_match("ab", std::regex("(ab|a)")) == true 

std::regex_match("ab", std::regex("(a|ab)")) == false 

我觉得true是在这两种情况下,逻辑正确。

锵& OSX:

$ cat > test.cpp 
#include <string> 
#include <regex> 
#include <iostream> 

int main() { 
    std::cout << std::regex_match("ab", std::regex("(a|ab)")) << std::endl; 
    std::cout << std::regex_match("ab", std::regex("(ab|a)")) << std::endl; 

    return 0; 
} 
^C 

$ clang++ -v 
Apple LLVM version 8.1.0 (clang-802.0.41) 
Target: x86_64-apple-darwin16.5.0 
Thread model: posix 
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin 

$ clang++ ./test.cpp -o test 

$ ./test 
0 
1 

锵& FreeBSD的:

$ cat > test.cpp 
#include <string> 
#include <regex> 
#include <iostream> 

int main() { 
    std::cout << std::regex_match("ab", std::regex("(a|ab)")) << std::endl; 
    std::cout << std::regex_match("ab", std::regex("(ab|a)")) << std::endl; 

    return 0; 
} 
^C 
$ clang++ -v 
FreeBSD clang version 3.8.0 (tags/RELEASE_380/final 262564) (based on LLVM 3.8.0) 
Target: x86_64-unknown-freebsd11.0 
Thread model: posix 
InstalledDir: /usr/bin 
$ clang++ ./test.cpp -o test 
$ ./test 
0 
1 

Linux的& GCC:

$ cat > test.cpp 
#include <string> 
#include <regex> 
#include <iostream> 

int main() { 
    std::cout << std::regex_match("ab", std::regex("(a|ab)")) << std::endl; 
    std::cout << std::regex_match("ab", std::regex("(ab|a)")) << std::endl; 

    return 0; 
} 
^C 

$ g++ -v 
Using built-in specs. 
COLLECT_GCC=g++ 
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper 
Target: x86_64-linux-gnu 
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.1-2ubuntu1~16.04' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --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-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-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 --enable-multilib --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 5.4.1 20160904 (Ubuntu 5.4.1-2ubuntu1~16.04) 

$ g++ -std=gnu++11 ./test.cpp -o test 

$ ./test 
1 
1 
+2

[GCC 6.3 test](https://ideone.com/gUm6cL)对于两者都返回1。 –

+2

[GCC 5.4.0测试返回1两也。(https://wandbox.org/permlink/0T73YQHHS3viGIzk) –

+3

为什么会*((的std :: regex_match( “AB”,的std ::正则表达式(“(一| ab)“))== false))* be * 0 *? –

回答

7

的ECMAScript(默认的正则表达式语法)试图匹配的替代品顺序,停在f第一次成功,这意味着在普通搜索(一个regex_search)正则表达式a|ab从来没有匹配整个ab;它总是匹配a部分。

该标准对于在这种情况下应该做什么而言含糊不清,导致实施分歧。请参阅LWG issue 2273以获取相应的解释。最终的标准进行了修订(见这个问题的解决)做出明确规定regex_match只考虑匹配整个输入序列可能的匹配,作为添加到标准的例子昭示:

std::regex re("Get|GetValue"); 
std::cmatch m; 
regex_search("GetValue", m, re); // returns true, and m[0] contains "Get" 
regex_match ("GetValue", m, re); // returns true, and m[0] contains "GetValue" 

原来<regex>实施在libC++中使用了另一种解释,然而,它只是没有更新以匹配分辨率,直到recently。铿锵4.0现在打印1 1