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)'
参见文档中的set operations。你可以这样做:
假设变量长度为一个字符:
看起来基本上你要在
x in
前面加上任何不是你的标记的东西。可能是这样的:它应该适用于被识别为单词的符号;如果您需要更具体的东西(即变量中有其他字符),您需要自己定义它,而不是使用
\w
。。。你知道吗相关问题 更多 >
编程相关推荐