gpt4 book ai didi

c++ - 查找两者之间的两个数字的正则表达式的匹配

转载 作者:行者123 更新时间:2023-12-02 10:37:45 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Difference between std::regex_match & std::regex_search?

(2 个回答)


2年前关闭。




我试图找到可以在字符串中找到两个数字的正则表达式,第一个数字由 10 个数字组成,第二个数字是 58 个(9091968376 和 8887918192818786347374918784939277359287883421333333338896)。这两个数字可以在字符串的任何位置。这是我到目前为止所拥有的。

#include <iostream>
#include <regex>

void dump_regex_matches(const std::cmatch& match)
{
std::cout << "Matches:\n";

for (std::size_t i = 0; i < match.size(); ++i)
std::cout << i << ": " << match.str(i) << '\n';
}

int main()
{
std::regex regex(R"(.*(\d{10}).*(\d{58}))");
std::string searchStr = "var a=0,m,v,t,z,x=new Array('9091968376','8887918192818786347374918784939277359287883421333333338896','778787','949990793917947998942577939317'),l=x.length;while(++a<=l){m=x[l-a];}";
std::cmatch match;
if (std::regex_match(searchStr.c_str(), match, regex))
{
std::cout << "We got match!\n";
dump_regex_matches(match);
}

return 0;
}

如果我没有在数字周围加上引号,但它没有引号。感谢任何帮助。

最佳答案

regex_match 检查完全匹配。添加 .*最后允许之后的任何事情:

std::regex regex(R"(.*(\d{10}).*(\d{58})).*");

或使用 regex_search 而不是 regex_match :
int main()
{
std::regex regex(R"((\d{10}).*(\d{58}))");
std::string searchStr = "var a=0,m,v,t,z,x=new Array('9091968376','8887918192818786347374918784939277359287883421333333338896','778787','949990793917947998942577939317'),l=x.length;while(++a<=l){m=x[l-a];}";
std::cmatch match;
if (std::regex_search(searchStr.c_str(), match, regex))
{
std::cout << "We got match!\n";
dump_regex_matches(match);
}

return 0;
}

关于c++ - 查找两者之间的两个数字的正则表达式的匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59519446/

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