使用Ruby我想下面的文字用正則表達式紅寶石正則表達式匹配,除非逃脫
~foo\~\=bar =cheese~monkey
裏劈〜或=表示比賽開始,除非它與\
逃脫因此,它應該符合
~foo\~\=bar
然後
=cheese
然後
~monkey
我想下面的工作,但事實並非如此。
([~=]([^~=]|\\=|\\~)+)(.*)
什麼是更好的正則表達式使用?
編輯更具體地,上述正則表達式的所有出現匹配=和〜
編輯工作溶液。這是我想出來解決這個問題。我發現Ruby 1.8展望未來,但不具有向後看功能。環顧了一下後,所以,我在comp.lang.ruby跨this post來了,和下面的完成它:
# Iterates through the answer clauses
def split_apart clauses
reg = Regexp.new('.*?(?:[~=])(?!\\\\)', Regexp::MULTILINE)
# need to use reverse since Ruby 1.8 has look ahead, but not look behind
matches = clauses.reverse.scan(reg).reverse.map {|clause| clause.strip.reverse}
matches.each do |match|
yield match
end
end
你可以發佈你的工作解決方案嗎?我想看看它。乾杯! ;-) – Tomalak 2008-12-17 07:25:00