To match shell-style wildcards, you can (ab) use the fnmatch module
Since fnmatch is the main one for matching file names, the test will be case-sensitive or case-insensitive. So you have to normalize both the text and the template (here I use lower() for this purpose)
>>> import fnmatch >>> pattern_list = ['abandon*', 'abuse*', 'abusi*', 'aching', 'advers*', 'afraid', 'aggress*'] >>> string_input = "People who have been abandoned or abused will often be afraid of adversarial, abusive, or aggressive behavior. They are aching to abandon the abuse and aggression." >>> for pattern in pattern_list: ... l = fnmatch.filter(string_input.split(), pattern) ... if l: ... print pattern, "match", l
Production:
abandon* match ['abandoned', 'abandon'] abuse* match ['abused', 'abuse'] abusi* match ['abusive,'] aching match ['aching'] advers* match ['adversarial,'] afraid match ['afraid'] aggress* match ['aggressive', 'aggression.']
source share