1

我试图找到一个正则表达式来匹配 C++ 头文件中非虚拟的方法声明。我在大多数情况下都可以使用它,但是如果在 virtual 之后有换行并且方法声明在下一行,我的正则表达式将匹配它,这不是我想要的。因此,我需要正则表达式引擎回溯到上一行并检查 virtual 关键字。这是一个基本示例:

源文件片段:

void Process(char* name, int val); // Should match, this one works
virtual char* GetName(); // Should not match since it is virtual, this one works
virtual
        flag_type SetValue(uint8 resourceIndex, char* name); // Should not match, but this line matches since virtual is on previous line!

这是我的正则表达式: (^\s*\w+\**(?<!virtual)\s+\w+\s*\()(\s*\w+\**\s+\**\w+,?)*\s*\)\s*;

4

1 回答 1

0

我发现了一些对我有用的东西,包括 Emanuele 指出的缺陷(尽管它也会在注释行中匹配):\b(?<!virtual\s+)\w+\**\s+\w+\s*\((\s*\w+\**\s+\**\w+,?)*\s*\)\s*;

于 2012-04-30T21:46:50.790 回答