什么是基于正则表达式的函数 f
,给定输入文本和字符串,返回文本中包含该字符串的所有单词。例如:
f("This is just a simple text to test some basic things", "si")
会返回:
["simple", "basic"]
(因为这两个词包含子串"si"
)
怎么做?
最佳答案
对于这样的事情我不会使用正则表达式,我会使用这样的东西:
def f(string, match):
string_list = string.split()
match_list = []
for word in string_list:
if match in word:
match_list.append(word)
return match_list
print f("This is just a simple text to test some basic things", "si")
关于Python 正则表达式 : return a list of words containing a given substring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29090167/