a = "Beauty Store is all you need!"
b = "beautystore"
test1 = ''.join(e for e in a if e.isalnum())
test2 = test1.lower()
test3 = [test2]
match = [s for s in test3 if b in s]
if match != []:
print(match)
>>>['beautystoreisallyouneed']
我想要的是:“美容店”
我在字符串中搜索关键字,我想从字符串中以字符串的原始格式(带有大写字母和空格,无论如何)返回关键字,但是仅返回包含关键字的部分。
答案 0 :(得分:1)
如果关键字仅出现一次,这将为您提供正确的解决方案:
a = "Beauty Store is all you need!"
b = "beautystore"
ind = range(len(a))
joined = [(letter, number) for letter, number in zip(a, ind) if letter.isalnum()]
searchtext = ''.join(el[0].lower() for el in joined)
pos = searchtext.find(b)
original_text = a[joined[pos][1]:joined[pos+len(b)][1]]
它保存每个字母的原始位置,将它们连接到小写字符串,找到位置,然后再次查找原始位置。