print 'cycle' ;While i in range(1,n) [[print "Number:" ;print i; print 'and,']]
例如,我有这样一条线。我只想从双方括号内的[[...]]子字符串中提取分号字符。
如果我使用re.search(\[\[.*(\s*;).*\]\])
,我只会得到一个分号。对此有适当的解决方案吗?
答案 0 :(得分:3)
;(?=(?:(?!\[\[).)*\]\])
模式分解:
; # match literal ";"
(?= # lookahead assertion: assert the following pattern matches:
(?:
(?!\[\[) # as long as we don't find a "[["...
. # ...consume the next character
)* # ...as often as necessary
\]\] # until we find "]]"
)
换句话说,模式会检查分号后跟]]
,但后面跟不是[[
。
模式不起作用的字符串示例:
; ]]
(将匹配)[[ ; "this is text [[" ]]
(不匹配)