gpt4 book ai didi

c++ - regex_match 找不到方括号

转载 作者:行者123 更新时间:2023-11-30 05:28:59 25 4
gpt4 key购买 nike

<分区>

我正在尝试对其中包含方括号 ([...]) 的字符串执行 regex_match。

到目前为止我尝试过的事情:

  • 正常匹配
  • 用 1 个斜杠反斜杠方括号
  • 用 2 个斜杠反斜杠方括号

重现代码:

#include <iostream>
#include <cstring>
#include <regex>

using namespace std;

int main () {
std::string str1 = "a/b/c[2]/d";
std::string str2 = "(.*)a/b/c[2]/d(.*)";
std::regex e(str2);

std::cout << "str1 = " << str1 << std::endl;
std::cout << "str2 = " << str2 << std::endl;
if (regex_match(str1, e)) {
std::cout << "matched" << std::endl;
}
}

这是我每次编译时得到的错误信息。

terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted (core dumped)

stack overflow 成员告诉我,gcc 4.8 或更早的版本已知存在错误。所以,我需要将它更新到最新版本。

我创建了一个 Ideone fiddle编译器不应该出现问题的地方。 即使在那里,我也没有看到 regex_match 发生。

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