regex - Python regexes: return a list of words containing a given substring -
what function f
based on regexes that, given input text , string, returns words containing string in text. example:
f("this simple text test basic things", "si")
would return:
["simple", "basic"]
(because these 2 words contain substring "si"
)
how that?
i'm not convinced there isn't better way approach, like:
import re def f(s, pat): pat = r'(\w*%s\w*)' % pat # not thrilled line return re.findall(pat, s) print f("this simple text test basic things", "si")
works:
['simple', 'basic']
Comments
Post a Comment