Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match C's multiline preprocessor statements

Tags:

python

regex

what I need is to match multiline preprocessor's statements such as:

#define max(a,b) \
       ({ typeof (a) _a = (a); \
           typeof (b) _b = (b); \
         _a > _b ? _a : _b; })

The point is to match everything between #define and last }), but I still can't figure out how to write the regexp. I need it to make it work in Python, using "re" module.

Could somebody help me please?

Thanks

like image 478
Martin Avatar asked Oct 23 '25 14:10

Martin


1 Answers

This should do it:

r'(?m)^#define (?:.*\\\r?\n)*.*$'

(?:.*\\\r?\n)* matches zero or more lines ending with backslashes, then .*$ matches the final line.

like image 55
Alan Moore Avatar answered Oct 25 '25 05:10

Alan Moore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!