RegEx匹配令牌并保持分隔符

时间:2014-07-25 09:44:08

标签: regex vb.net

我正在使用此RegEx(vb.net)来匹配字符串的所有标记并保留分隔符(单独的捕获组):

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

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

输出:

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

如何使用?作为转义分隔符(奇数?)来实现相同的目标?

Text1+Text2?:Text3~

应该导致

Text1 + Text2?:Text3 ~

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

试试这个:

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

Demo


这不一定会将?a转移到a??转换为?,因此您需要在没有正则表达式的情况下进行一些后期处理。但是,?将有效地逃避下一个角色。因此:?:不会是分隔符,??:将是分隔符,???:将不是分隔符。


说明:

(           (?# 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)
相关问题