开发者

string, regular expressions in python

开发者 https://www.devze.com 2023-02-19 07:39 出处:网络
From the following question How to automatically remove certain preprocessors directives and comments from a C header-file?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号