gpt4 book ai didi

C++ 标准正则表达式问号问题

转载 作者:太空狗 更新时间:2023-10-29 21:02:13 24 4
gpt4 key购买 nike

我在使用 std 正则表达式时遇到问题。我无法使问号量词起作用。对 regex_match 的调用将始终返回 0。

我还尝试了 {0,1},它的行为也不像我预期的那样:它的行为就像一个 + 量词。

这是我的代码:

#include <iostream>
#include <regex>
using namespace std;

int main(int argc, char **argv){

regex e1("ab?c");
cout << regex_match("ac", e1) << endl; // expected : 1, output 0
cout << regex_match("abc", e1) << endl; // expected : 1, output 0
cout << regex_match("abbc", e1) << endl; // expected : 0, output 0

regex e2("ab{0,1}c");
cout << regex_match("ac", e2) << endl; // expected : 1, output 0
cout << regex_match("abc", e2) << endl; // expected : 1, output 1
cout << regex_match("abbc", e2) << endl; // expected : 0, output 1

return 0;
}

我使用了下面的命令来编译:

g++ -std=c++11 main.cpp -o regex_test

我是不是做错了什么?或者为什么它不起作用?

最佳答案

您的正则表达式代码没问题。您正在使用的实现不是。它提供了一个 header ,声明了库实现不佳的一堆东西。如果一个商业包这样做,它会受到严厉的批评,这是正确的。一分钱一分货。

关于C++ 标准正则表达式问号问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15987278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com