panc_fab
panc_fab

Reputation: 530

Using regex to filter for preprocessor directives?

I have to filter out from a huge amount of cpp files all the macros defined, excluding the guards, for example:

#if <NAME>
#ifdef <NAME>
#ifndef <NAME>
#if defined(<NAME>)
#if defined <NAME>
!defined(<NAME>)
!defined <NAME>
#else if <NAME>
#elif <NAME>

I have to retrieve all the NAMEs, but they are not all in the form XXX, due to different programmers working on the project, there are a lot of definitions, so I am facing problems in define a regex that can extract only <NAME> from each of the situations just described.

Any advice is appreciated!

EDIT As someone pointed out, my NAME (with surrounding angle brackets <>) is only a placeholder, where in reality it can be XXXX, XXXX, XX_Y, _XXX , _XXX_Y, XXYY where X and Y could be uppercase letters or digits, with no regularity in the name! They are directives to the preprocessor and I have to filter them out.

Upvotes: 0

Views: 1882

Answers (1)

SimonC
SimonC

Reputation: 1618

Quickly tested this using http://regexr.com with the examples you provided. Matches most of the cases.

You might have to refine it a little.

([#!][A-z]{2,}[\s]{1,}?([A-z]{2,}[\s]{1,}?)?)([\\(]?[^\s\\)]{1,}[\\)]?)?

Quick explanation:

([#!][A-z]{2,}[\s]{1,}?([A-z]{2,}[\s]{1,}?)?)

Matches (most) strings beginning with a '#' or '!', and a directive. A second word is also allowed, whitespaces are ignored (it will match with and without n whitespace)

([\(]?[^\s\)]{1,}[\)]?)?

Will match both bracketed and none-bracketed strings. Will not match if whitespace is inside the brackets.

If you want to match whitespace inside the brackets, change ^\s\) to ^\)

Update Some of the backspaces weren't displayed in the answer. Reserved characters e.g.: []{}(), etc must be escaped. Fixed the answer. Might have missed one or two, sorry in that case.

Update 05.03.2020 @gregn3 has provided an updated version in the comments which allows for whitespace between the # and the following word.

([#!][ \t]*[A-z]{2,}[\s]{1,}?([A-z]{2,}[\s]{1,}?)?)([\\(]?[^\s\\)]{1,}[\\)]?)?

Upvotes: 3

Related Questions