gpt4 book ai didi

C++ regex_match 不匹配

转载 作者:行者123 更新时间:2023-11-27 23:06:13 26 4
gpt4 key购买 nike

我在 XCode 上使用 C++。我想使用 regex_match 匹配非字母字符,但似乎有困难:

#include <iostream>
#include <regex>

using namespace std;

int main(int argc, const char * argv[])
{
cout << "BY-WORD: " << regex_match("BY-WORD", regex("[^a-zA-Z]")) << endl;
cout << "BYEWORD: " << regex_match("BYEWORD", regex("[^a-zA-Z]")) << endl;

return 0;
}

返回:

BY-WORD: 0
BYEWORD: 0

我希望匹配“BY-WORD”(因为有连字符),但 regex_match 为两个测试返回 0。

我糊涂了。

最佳答案

regex_match 尝试将整个输入字符串与您提供的正则表达式进行匹配。由于您的表达式只会匹配单个字符,因此在这些输入上它总是会返回 false。

你可能想要 regex_search相反。

关于C++ regex_match 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368050/

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