2013-05-22 75 views
0

我想解析多行註釋,但它總是貪婪的。貪婪的正則表達式 - 如何支持多行模式?

正則表達式:

MUL_COMMENT ("#*"(.|\n)*?"*#") 

撓曲文件片段:

<DIRECTIVE>{MUL_COMMENT} {BEGIN INITIAL; 
    printf("mul comment for directive end:%s\n",yytext);yylval.string = strdup(yytext); return COMMENT;} 

該文本文件:

#*erewrewrewr 
wer*# 

#set($a=7) 

#*#ere 
wrewrewrwe*# 

的解析結果:

Node:astn=Comment,image:"#*erewrewrewr\nwer*#\n\n#set(   $a=7)\n\n#*#ere\nwrewrewrwe*#" 

mul comment for directive end:#*erewrewrewr 
wer*# 

#set($a=7) 

#*#ere 
wrewrewrwe*# 

圖像是他的字符串匹配,顯然他是貪婪!如何解決它,請幫助我!

回答

2

通常的方法是不匹配*#您的評論裏:

MUL_COMMENT "#*"([*]*[^*#]|[#])*"*#" 
+0

它運作良好,非常感謝你! – sinory