Python 正则表达式 - 在子字符串中查找多个字符
Python regex - Find multiple characters in a substring
print 'cycle' ;While i in range(1,n) [[print "Number:" ;print i; print 'and,']]
例如,我有这样一行。我只想从双方括号内的 [[ ... ]] 子字符串中提取分号字符。
如果我使用 re.search(\[\[.*(\s*;).*\]\])
,我只会得到一个分号。有没有合适的解决方案?
正则表达式从来都不是像这样的事情的好选择,因为它很容易出错,但以下模式适用于普通情况:
;(?=(?:(?!\[\[).)*\]\])
模式分解:
; # 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 [[" ]]
(不匹配)
print 'cycle' ;While i in range(1,n) [[print "Number:" ;print i; print 'and,']]
例如,我有这样一行。我只想从双方括号内的 [[ ... ]] 子字符串中提取分号字符。
如果我使用 re.search(\[\[.*(\s*;).*\]\])
,我只会得到一个分号。有没有合适的解决方案?
正则表达式从来都不是像这样的事情的好选择,因为它很容易出错,但以下模式适用于普通情况:
;(?=(?:(?!\[\[).)*\]\])
模式分解:
; # 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 [[" ]]
(不匹配)