擅长:python、mysql、java
<p>您当前的问题是<code>.+</code>是贪婪的,因此它匹配<code>@s0 else</code>,而不是{<cd3>}。要使其非贪心,请改用<code>.+?</code>:</p>
<pre><code>import re
regex_cond = re.compile(r'.+?(else\s+if|else|if).+')
line2 = '@s0 else if (insreg==1) then (initx,PING,notend) -> sinitx;'
match = regex_cond.match(line2)
print(match.groups())
# ('else if',)
</code></pre>
<p>然而,正如其他人所建议的那样,使用<a href="http://pyparsing.wikispaces.com/" rel="nofollow noreferrer">Pyparsing</a>这样的解析器比在这里使用<code>re</code>更好。在</p>