问题描述
我有一个这样的字符串
"yJdz:jkj8h:jkhd::hjkjh"
我想使用冒号作为分隔符来分割它,而不是双冒号.预期结果:
I want to split it using colon as a separator, but not a double colon. Desired result:
("yJdz", "jkj8h", "jkhd::hjkjh")
我正在尝试:
re.split(":{1}", "yJdz:jkj8h:jkhd::hjkjh")
但我得到了错误的结果.
but I got a wrong result.
与此同时,我正在使用 string.replace("::", "$$")
In the meanwhile I'm escaping "::"
, with string.replace("::", "$$")
推荐答案
您可以在 (?<!:):(?!:)
上拆分.这使用了两个 negative lookarounds(后视和前瞻),它们断言仅有效匹配有一个冒号,前后没有冒号.
You could split on (?<!:):(?!:)
. This uses two negative lookarounds (a lookbehind and a lookahead) which assert that a valid match only has one colon, without a colon before or after it.
解释模式:
(?<!:) # assert that the previous character is not a colon
: # match a literal : character
(?!:) # assert that the next character is not a colon
两种环视都需要,因为如果只有后视,那么正则表达式引擎将匹配 ::
中的第一个冒号(因为前一个字符不是冒号),如果只有前瞻,第二个冒号会匹配(因为下一个字符不是冒号).
Both lookarounds are needed, because if there was only the lookbehind, then the regular expression engine would match the first colon in ::
(because the previous character isn't a colon), and if there was only the lookahead, the second colon would match (because the next character isn't a colon).
这篇关于使用正则表达式用单冒号而不是双冒号拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!