2017-04-18 70 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