编写C ++正则表达式以匹配#include预处理指令 [英] Write c++ regular expression to match a #include preprocessing directive
问题描述
我想用c ++写一个正则表达式来匹配#include预处理指令.所以我写了这个:
I want to write a regular expression in c++ to match a #include preprocessing directive. So I wrote this:
std::regex includePattern("^[[:blank:]|[:space:]]*#[[:blank:]|[:space:]]*include[[:blank:]|[:space:]]+[<|\"]{1}[_[:alpha:]]+[_[:alnum:]]*");
这适用于:
std::string matchString = "#include <vector>";
但仅匹配字符串的一部分,但不包括结尾的>",但是如果我将正则表达式更改为此:
But only matches parts of the string excluded the trailing ">", but if I change the regex to this:
std::regex includePattern("^[[:blank:]|[:space:]]*#[[:blank:]|[:space:]]*include[[:blank:]|[:space:]]+[<|\"]{1}[_[:alpha:]]+[_[:alnum:]]*[>|\"]{1}");
它只是不会给我想要的结果,只是告诉我未找到"!有什么问题吗?
It just won't give me the desired result, just tell me "not found" ! Is there anything wrong?
有人可以帮我用c ++编写一个准确的正则表达式来匹配#include预处理指令吗?
Can anybody help me to write an accurate regular expression in c++ to match a #include preprocessing directive?
提前谢谢!
推荐答案
这取决于要匹配的输入是否包含新的线? [[:: space:]]
将匹配任何空格,包括新的空格行, [[[:blank:]]
将匹配除换行以外的任何空白(而且我不确定该标准是否支持该功能).反正,像这样:
It depends on whether the input to be matched may contain new
lines? [[:space:]]
will match any white space, including new
lines, [[:blank:]]
will match any white space except new lines
(and I'm not sure it is supported by the standard). Anyway,
something like:
"^\\s*#\\s*include\\s+[<\"][^>\"]*[>\"]\\s*"
应该可以解决问题,但是...
should do the trick, but...
-
如果您的来源中有不应该包含的新行,则仍然可能匹配.
If your source has new lines where it shouldn't, it still might match.
如果您的来源转义了新行,请在令牌 include
或文件名,它将不匹配.(这是合法的C ++,但没有人愿意这样做.)
If your source has escaped new lines, say in the middle of the
token include
or the file name, it won't match. (This is
legal C++, but no one in their right mind would do it.)
如果您的源中的分隔符不匹配,请在一端加上一个"
,以及另一个<
或<
,它们仍然会匹配.
If your source has mismatched delimiters, a "
at one end,
and a <
or a <
at the other, it will still match.
并且它不处理行尾的注释.处理方式C ++样式注释(//
)只需添加即可(?? .. *)?"
到表达式的末尾.处理C风格评论(特别是因为可以有多个评论)要多一些
And it doesn't handle comments at the end of line. Handling
C++ style comments (//
) should only be a matter of adding
"(?://.*)?"
to the end of the expression. Handling C style
comments (particularly since there can be several) is a bit more
complicated.
为确保定界符匹配,您可能需要输入包含在或之后的所有内容:
To ensure that the delimiters match, you'd probably have to put everything after the include in an or:
"^\\s*#\\s*include\\s+(?:<[^>]*>|\"[^\"]*\")\\s*"
同样,您需要添加到末尾以处理评论.
Again, you'd need to add to the end to handle comments.
这篇关于编写C ++正则表达式以匹配#include预处理指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!