2014-07-25 118 views
1

我使用這個表達式(vb.net)來匹配字符串的所有標記,並保持分隔符(單獨捕獲組):正則表達式匹配的令牌,並保持分隔符

([^~\+\:]*)([~\+\:])

Text1+Text2::Text4::Text6~Text1+Text2:Text3+Text4~

輸出:

Text1 + Text2 : : Text4 : : Text6 ~ Text1 + Text2 : Text3 + Text4 ~

我怎樣才能實現與相同0作爲轉義分隔符(奇數?)?

Text1+Text2?:Text3~

應導致

Text1 + Text2?:Text3 ~

感謝您的幫助

+0

什麼會'文本1 +文本2 ??:文本3〜'返回?我的猜測是:'Text1 +','Text2? :'和'Text3〜'... – Sam

+0

['(。*?)((?<![?])[〜\ + \:])'](http://regex101.com/r/bK8tH5/1)? – Sam

+0

謝謝。但是這對多個'?'不起作用,'(。*?)'是一個性能殺手。 – user3525398

回答

0

試試這個:

((?:\?.|[^~+:])*)([~+:]) 

Demo


這不一定會逃脫?aa???,所以你需要做一些後期處理沒有正則表達式。然而,?將有效逃脫下一個角色。所以:?:不會是分隔符,??:將是分隔符,???:不會是分隔符。


說明:

(   (?# start capture group for text) 
    (?:  (?# start non-capture group for repeating alternation) 
    \?.  (?# match ? literally followed by any character -- escaping) 
    |  (?# OR) 
    [^~+:] (?# match any non-delimiter characters ~, +, and :) 
)*  (?# repeat non-capture group 0+ time) 
)   (?# end capture group) 
(   (?# start capture group for delimiter) 
    [~+:]  (?# match any delimiter character ~, +, and :) 
)   (?# end capture group) 
+0

這應該完全測試..如果有什麼不對,或者如果您有任何問題,請告訴我。 – Sam

+0

看起來不錯。再次感謝! – user3525398

相關問題