字符串,python中的正则表达式

时间:2011-03-23 09:21:35

标签: python string

来自以下问题 How to automatically remove certain preprocessors directives and comments from a C header-file?

header = "" #some string

p_macro = re.compile("#if.*?#endif", re.MULTILINE + re.DOTALL)  
p_comment = re.compile("/\*.*?\*/", re.MULTILINE + re.DOTALL)   


# Example ...
# print re.sub(p_macro, '', header)
# print re.sub(p_comment, '', header)

然而,这会导致像

这样的情况失败
#endif // #if 0

可以在re表达式中添加什么来避免这种情况?

1 个答案:

答案 0 :(得分:1)

p_macro = re.compile("#(end)?if.*?#(?(1)|end)if",re.DOTALL)

re.MULTILINE没用,因为RE中没有字符'^'和'$'

可能,你必须无休止地加上这样的修正......