开发者

Perl style regular expression for C comments

开发者 https://www.devze.com 2023-04-07 09:06 出处:网络
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 \

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

精彩评论

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

关注公众号