string, regular expressions in python
From the following question How to automatically remove certain preprocessors directives and comments from a C header-file?
header = "" #some string
p_macro = re.compile("#if.*?#endif", re.MULT开发者_如何学GoILINE + re.DOTALL)
p_comment = re.compile("/\*.*?\*/", re.MULTILINE + re.DOTALL)
# Example ...
# print re.sub(p_macro, '', header)
# print re.sub(p_comment, '', header)
This however results in a failure for a case like
#endif // #if 0
What could be added in the re expression to avoid this?
p_macro = re.compile("#(end)?if.*?#(?(1)|end)if",re.DOTALL)
re.MULTILINE is useless because there is no character '^' and no '$' in the RE
Possibly, you will have to add such correction endlessly.....
精彩评论