我有一个作为字符串的源词,我有一个字符串,人们在其中输入了一个词列表,但我如何检查输入的字符串是否只包含源词中的字符,但顺序不限
def check(input_string):
import re
#http://docs.python.org/library/re.html
#re.search returns None if no position in the string matches the pattern
#pattern to search for any character other then . a-z 0-9
pattern =word
if re.search(pattern, test_str):
#Character other then . a-z 0-9 was found
print('Invalid : %r' % (input_string,))
else:
#No character other then . a-z 0-9 was found
print('Valid : %r' % (input_string,))```
答案 0 :(得分:4)
使用支持检查子集的set
。
template = set(word)
if set(input_string) < template:
print("OK")
如果你坚持使用正则表达式,把模板变成字符类:
template = re.compile(f'[{word}]+')
if template.fullmatch(input_string):
print("OK")
答案 1 :(得分:0)
定义乐趣():
s = "Test" # Word
b = "TEST" #input_string
a = True
for c in b:
if c.lower() not in s.lower():
a = False
break
if (a == False):
print("Character is not in s")
else:
print("No Other characters found")