基于正则表达式的函数f
是什么,给定输入文本和字符串,并返回文本中包含此字符串的所有单词。例如:
f("This is just a simple text to test some basic things", "si")
会返回:
["simple", "basic"]
(因为这两个词包含子字符串"si"
)
怎么做?
python大神给出的解决方案
我不相信没有比我的方法更好的方法了,但是类似:
import re
def f(s, pat):
pat = r'(\w*%s\w*)' % pat # Not thrilled about this line
return re.findall(pat, s)
print f("This is just a simple text to test some basic things", "si")
作品:
['simple', 'basic']