2011-03-23 96 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沒有「$」

可能的話,你將不得不無休止地增加這種修正.....