问题描述
如果它的值在括号内,我需要检查我的项目文件中的每个“#define”。 例如:
#define A (50)
#define B (60)
#define C 70
我想找出所有包含错误格式 #define 的文件,以便手动更正它。
我想通过运行一个小的 powershell 脚本来自动执行此任务,但我一直在实施它。这是我目前写的:
GetChildItem -Recurse | Select-String "#define" /*some additional code*/ -List | Select Path
任何解决方案将不胜感激。
解决方法
类似于:
... |Select-String '^#define(?!\s+[\w]+\s+\([^\)]+\)\s*$)'
...应该这样做。
正则表达式模式分解为:
^ # start of line
#define # followed by literal string "#define"
(?! # negative look-ahead for
\s+ # 1 or more whitespace chars
[\w]+ # 1 or more words characters
\s+ # 1 or more whitespace chars
\([^\)]+\) # literal "(" followed by 1 or more non-"(",and finally a literal ")"
\s* # trailing whitespace
$ # end of string
) # end negative look-ahead
换句话说,我们正在寻找符合以下条件的行:
- 有一个
#define
指令, - 哪个不格式良好