我的代码是:
import re
x=["Set","Sets","ShowSets","Union","Intersect","SetUnion","SetIntersect"]
print (x)
while True:
text_to_search = input('Introduce an instruction: ')
for match1 in re.finditer('Sets?|ShowSet|ShowSets|Union|Intersect|SetUnion|SetIntersect',text_to_search):
print("Instruction: ")
#x.append(match1)
print(match1)
for match2 in re.finditer(r':=|{|}|;',text_to_search):
print("Operator: ")
print(match2)
for match3 in re.finditer(r'[a-zA-Z0-9]+',text_to_search):
if match3 in x:
pass
else:
print("ID: ")
print(match3)
print(x)我想要做的是打印所有不是指令的单词(避免打印集、集、ShowSets等),但问题是没有执行if match3 in x:,所以程序正在打印:“Set”、“Sets”、“ShowSets”、“Union”、“Intersect”、“SetUnion”、“SetIntersect”
Introduce an instrucción: Set Hi
Instruction:
<_sre.SRE_Match object; span=(0, 3), match='Set'>
ID:
<_sre.SRE_Match object; span=(0, 3), match='Set'>
ID:
<_sre.SRE_Match object; span=(4, 6), match='Hi'>
['Set', 'Sets', 'ShowSets', 'Union', 'Intersect', 'SetUnion', 'SetIntersect']而不是:
Introduce an instrucción: Set Hi
Instruction:
<_sre.SRE_Match object; span=(0, 3), match='Set'>
ID:
<_sre.SRE_Match object; span=(4, 6), match='Hi'>
['Set', 'Sets', 'ShowSets', 'Union', 'Intersect', 'SetUnion', 'SetIntersect']发布于 2018-05-26 17:13:06
首先提取匹配的文本:
if match3.group() in x:https://stackoverflow.com/questions/50545172
复制相似问题