gpt4 book ai didi

C++正则表达式搜索多行注释(/* */之间)

转载 作者:太空宇宙 更新时间:2023-11-04 14:50:05 43 4
gpt4 key购买 nike

<分区>

我正在尝试实现简单的案例(基本上是在两个标签之间查找文本,无论它们是什么)。我要排队

/* 我的评论1 */

/* 我的评论2 */

/* 我的评论3 */

作为输出。似乎我需要将捕获组限制为 1?因为在字符串 Hello/* my comment 1 */world 上我得到了我想要的 - res[0] contains/* my comment 1 */

#include <iostream>
#include <string>
#include <regex>

int main(int argc, const char * argv[])
{
std::string str = "Hello /* my comment 1 */ world /* my comment 2 */ of /* my comment 3 */ cpp";

std::cmatch res;
std::regex rx("/\\*(.*)\\*/");

std::regex_search(str.c_str(), res, rx);

for (int i = 0; i < sizeof(res) / sizeof(res[0]); i++) {
std::cout << res[i] << std::endl;
}

return 0;
}

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