gpt4 book ai didi

c++ - Pcrecpp 比赛

转载 作者:行者123 更新时间:2023-11-30 02:56:56 26 4
gpt4 key购买 nike

我使用 pcrecpp c++​​ (PCRE lib)
我需要循环获取所有匹配项。我该怎么做?

例如模式:
“你好”

和主题:
“你好你好”

循环应该循环 3 次(因为 3 次匹配)
1 你好
2 你好
3 你好

伪代码

pcrecpp::RE pPattern ( "hello" );  
std::string strBase = "hello hello hello";
// ...
int iMatches = // Match count
for ( int i = 1; i < iMatches; i++ )
{
printf( "%d %s", i, pPattern[ i ].c_str () );
}

请给我一些如何使用 pcrecpp.h 完成的示例代码。
对不起,我的英语不好。

最佳答案

即使这个问题已经有几个月了,我还是会在这里提供一个解决方案:

#include <pcrecpp.h>
#include <iostream>

int main(void)
{
pcrecpp::RE regex("(hello)");
std::string strBase = "hello hello hello";

pcrecpp::StringPiece input(strBase);

std::string match;

int count = 0;
while (regex.FindAndConsume(&input, &match)) {
count++;
std::cout << count << " " << match << std::endl;
}
}

欲了解更多信息,this site可能有帮助。

关于c++ - Pcrecpp 比赛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15057873/

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