c++正则表达式搜索多行注释(在/* */之间)
C++ regex search for multi-line comments (between /* */)
我试图实现简单的情况(基本上找到两个标签之间的文本,无论它们是什么)。我想要得到lines
/* my comment 1 */
/* my comment 2 */
/* my comment 3 */
作为输出。似乎我需要将捕获组限制为1?因为在字符串Hello /* my comment 1 */ world
上我得到了我想要的- res[0]包含/*我的注释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;
}
通过将量词*
转换为其非贪婪版本,使正则表达式仅匹配到*/
的第一个出现。这是通过在后面添加一个问号来实现的:
std::regex rx("/\*(.*?)\*/");
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- C++正则表达式无限循环
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 定义有趣的宏和正则表达式在Z3 C++绑定
- 带有多个字符分隔符的正则表达式
- C++ 使用增强正则表达式库时断言崩溃
- 有人可以帮助我处理正则表达式吗?
- 使用正则表达式获取大括号块的列表
- 使用正则表达式c++从单词和分隔符之间的字符串中提取所有子字符串
- 正则表达式 获取两个换行符之间的文本
- 正则表达式匹配字符串之间的数字
- 正则表达式使用提升令牌迭代器在单引号和括号之间提取值
- 提升正则表达式 cpp 用于查找 %% 之间的字符串,输出不包括 % 字符本身
- 用于提取[]之间内容的正则表达式
- 正则表达式,获取两个关键字之间的整个字符串
- 如何使用正则表达式查找特殊字符串之间的所有数字
- 使用正则表达式替换oracle中特殊字符之间的字符串
- 在c++中使用正则表达式查找[/和]之间的数字
- c++正则表达式搜索多行注释(在/* */之间)