2011-03-23 86 views
-1

正则表达式从以下问题 How to automatically remove certain preprocessors directives and comments from a C header-file?字符串,在Python

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 

什么能在重新表达被添加到避免这种情况?

+3

这就是正则表达式不解析的原因。 – 2011-03-23 09:24:46

+0

新名称?多么傲慢 – 2011-03-23 14:45:58

回答

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

re.MULTILINE是无用的,因为没有字符“^”和在RE没有“$”

可能的话,你将不得不无休止地增加这种修正.....