user964210
user964210

Reputation: 143

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 the BA string.

Any guidance would be greatly appreciated.

Upvotes: 2

Views: 1024

Answers (2)

Justin Morgan
Justin Morgan

Reputation: 30690

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.

Upvotes: 3

nmichaels
nmichaels

Reputation: 50991

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".

Upvotes: 0

Related Questions