我試圖創造一個Python正則表達式,兜攬,這將匹配形式用於匹配單行和多行註釋的Python正則表達式。
// some comment
和
/* comment
more comment */
所以,我想
t_COMMENT = r'//.+ | /\*.+\*/'
,但是這並未評論不允許多行註釋,當我嘗試使用'點匹配所有'選項解決此問題時,如
t_COMMENT = r'//.+ | (?s) /\*.+\*/'
它導致'//'註釋類型匹配很多行。另外,如果我嘗試有兩個獨立的正則表達式像
t_COMMENT = r'//.+'
t_COMMENT2 = r'(?s) /\*.+\*/'
的「//」註釋類型仍然匹配多行,就好像點匹配的所有選項。
有人知道如何解決這個問題嗎?
我強烈懷疑這是一個壞主意。嘗試使用複雜的正則表達式進行太多高級別(語法)解析是創建詞法分析器/解析器系統的對立面。我建議你有單行註釋的標記,多行開始和結束。圍繞解析器構建的語法可以放棄在多行開始和結束之間找到的所有輸入。 – 2014-09-13 12:17:25
你能根據你的想法給出答案嗎? – Zvika 2014-09-28 15:40:40