小编SEA*_*unt的帖子

编写 C++ 正则表达式来匹配 #include 预处理指令

我想用 C++ 编写一个正则表达式来匹配 #include 预处理指令。所以我写了这个:

std::regex includePattern("^[[:blank:]|[:space:]]*#[[:blank:]|[:space:]]*include[[:blank:]|[:space:]]+[<|\"]{1}[_[:alpha:]]+[_[:alnum:]]*");
Run Code Online (Sandbox Code Playgroud)

这适用于:

std::string matchString = "#include <vector>";
Run Code Online (Sandbox Code Playgroud)

但仅匹配字符串的一部分,排除了尾随的“>”,但如果我将正则表达式更改为:

std::regex includePattern("^[[:blank:]|[:space:]]*#[[:blank:]|[:space:]]*include[[:blank:]|[:space:]]+[<|\"]{1}[_[:alpha:]]+[_[:alnum:]]*[>|\"]{1}");
Run Code Online (Sandbox Code Playgroud)

它只是不会给我想要的结果,只是告诉我“未找到”!有什么不对的吗?

任何人都可以帮助我在 c++ 中编写准确的正则表达式来匹配 #include 预处理指令吗?

提前致谢!

c++ regex include

2
推荐指数
1
解决办法
2170
查看次数

标签 统计

c++ ×1

include ×1

regex ×1