这是 clang c++11 std::regex_match 的功能还是错误?

Is that a feature or a bug of clang c++11 std::regex_match?

本文关键字:功能 错误 c++11 clang std regex 这是 match      更新时间:2023-10-16

我注意到,如果第一个模式是第二个模式的开始部分,则包含两个带有 OR 条件的模式的正则表达式与示例字符串不匹配(在 clang 3.5 和 clang 3.8 上测试):

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

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

我认为true这两种情况下在逻辑上都是正确的。

Clang & 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

Clang & 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

ECMAScript(默认的正则表达式语法)试图按顺序匹配替代项,在第一次成功时停止,这意味着在普通搜索(regex_search)中,正则表达式a|ab永远不会匹配整个ab;它总是只匹配a部分。

在这种情况下,该标准对regex_match应该做什么模棱两可,导致实施分歧。有关相互竞争的解释,请参见LWG第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"

然而,libc++ 中的原始<regex>实现使用了另一种解释,直到最近才更新以匹配分辨率。Clang 4.0 现在打印1 1.