![](/img/trans.png)
[英]python find all matches of multiple strings in a given string using regex?
[英]find all strings from list which matches with given few alphabets
我正在构建一个简单的函数来搜索列表中的一些数据。 我正在尝试从列表中搜索字符串与start of string
某些字符匹配。
喜欢 :-
my_list = ["com truise", "bill james", "bill bates", "bustin jeiber"]
def search_from_list(searched_word):
for word in my_list:
if searched_word == word:
print("Matched")
print("All words" + __full_word_that_matched__)
search_from_list("bi")
如果用户使用参数"bi"
调用该函数,那么我将尝试获取与该字符串"bi"
匹配的字符串,例如:-
["bill james", "bill bates"]
但这不起作用,我尝试了很多方法,例如:-
def search_from_list(searched_word):
my_list = ["com truise", "bill james", "bill bates", "bustin jeiber"]
print re.findall(r"(?=("+'|'.join(my_list)+r"))", searched_word)
searched_word("bi")
但它返回空
然后我尝试了
def search_from_list(searched_word):
words = re.findall(r'\w+', searched_word)
return [word for word in words if word in my_list]
n = search_from_list("bi")
它还向我显示了空列表
任何帮助将非常感激。 先感谢您
my_list = ...
def search_from_list(substring):
return [string for string in my_list if substring in string]
用正则表达式
import re
my_list = ["com truise", "bill james", "bill bates", "bustin jeiber"]
def search_from_list(searched_word):
return [word for word in my_list if re.match(searched_word, word)]
print(search_from_list("bi"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.