David Dai
David Dai

Reputation: 1145

Regexp for C preprocessor command

I am trying to write one regexp to match C preprocessor commands in C program. I wonder if you can give me some suggestions?

Thank you so much in advance.

Upvotes: 0

Views: 1352

Answers (3)

Jakob Riedle
Jakob Riedle

Reputation: 2018

This regex illustrates how the backslash \ newline can be integrated using recursive regex:

#(?<line>[^#].*?(\n|(\\[^\n]*\n(?&line))))

Hope this helps.

Upvotes: 0

P0W
P0W

Reputation: 47794

May be this: (not too exact though)

\s*#\s*(define|error|import|undef|elif|if|include|using|else|ifdef|line|endif|ifndef|pragma)\s*\S*

You can use cpp and pass the option -dM to list out all defined macros.

cpp -dM test.c

Upvotes: 0

Jens
Jens

Reputation: 72639

That would be

 grep '^[[:blank:]]*#'

Note that this will only grep the first line of a multi line preprocessor directive (continued with backslash newline).

Upvotes: 1

Related Questions