开发者

Perl style regular expression for C comments

At work, I have a requirement to create a perl-style regular expression for C comments (/*) for comments left in our code. Our business analysts had new requirements, and these were all prefaced with "BA", and I'm supposed to somehow scan the comments to find these instances. I am very unfamiliar with regular expressions and after reading more about them, I'm lost as to how to target comment blocks with only th开发者_如何学Goe BA string.

Any guidance would be greatly appreciated.


I'm not aware of any weird escaping rules for C comments, so I think you just want something like this:

/\/\*.*?\/\*/s

The s flag means that the . will also match carriage returns, so the comments can cross multiple lines.

To match only comments starting with "BA", you'd want:

/\/\*BA.*?\/\*/s

Consider adding the i flag if the "BA" part can be lowercase.


find . -regex ".*\.[ch]" | xargs grep -regex "/\* *BA"

What that does:

  • Find all your C source and header files.
  • Search each of those files for "/*" followed by any amount of whitespace, followed by "BA".
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜