这是 clang c++11 std::regex_match 的功能还是错误?
Is that a feature or a bug of clang c++11 std::regex_match?
我注意到,如果第一个模式是第二个模式的开始部分,则包含两个带有 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
.
相关文章:
- Malloc后的自由功能错误
- 功能错误中的表达语法帮助!C/C
- 将对象传递到类,没有接收匹配功能错误
- 无效打印功能错误
- 绑定std ::功能错误
- 我可以启用有关我自己的变异功能错误使用的指定符的警告
- 功能错误的使用无效
- C 朋友模板功能错误
- 单链接列表推送功能错误
- ConstexPR静态模板功能:G 错误是Clang上的警告
- 一个奇怪的打印功能错误
- 信号功能错误
- 查找最低值.功能.错误:线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x7fff5fc89000)
- 超载功能错误消息
- 与操作员'<<'不匹配 有人可以帮助解决无效功能错误吗?[C++]
- 没有操作员发现升压read_xml功能错误
- 工厂设计模式中的纯虚拟功能错误
- 如何跟踪LLVM验证功能错误"Expected no forward declarations!"?
- OOP项目功能错误
- c++ GTK控制台日志功能错误