[英]RegEx to match tokens and keep delimiter
我正在使用此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 ~
谢谢你的帮助
尝试这个:
((?:\?.|[^~+:])*)([~+:])
这不一定会将?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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.