擅长:python、mysql、java
<p>看起来基本上你要在<code>x in</code>前面加上任何不是你的标记的东西。可能是这样的:</p>
<pre><code>tokens = ['and', 'or', 'not']
grouping = ['(', ')']
def resub(match):
matchval = match.group(0)
if matchval in tokens:
return matchval
return 'x in %s'%matchval
s = "(A or B) and not(A and C)"
re.sub('\w+', resub, s)
'(x in A or x in B) and not(x in A and x in C)'
</code></pre>
<p>它应该适用于被识别为单词的符号;如果您需要更具体的东西(即变量中有其他字符),您需要自己定义它,而不是使用<code>\w</code>。。。你知道吗</p>