本文介绍了字符串中的Python通配符搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个列表
list = ['this','is','just','a','test']
如何让用户进行通配符搜索?
how can I have a user do a wildcard search?
搜索词:'th_s'
会返回'this'
推荐答案
正则表达式可能是解决这个问题最简单的方法:
Regular expressions are probably the easiest solution to this problem:
import re
regex = re.compile('th.s')
l = ['this', 'is', 'just', 'a', 'test']
matches = [string for string in l if re.match(regex, string)]
这篇关于字符串中的Python通配符搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!