Substring Detection? Pythonically?

Jeffrey P Shell jeffrey at Digicool.com
Wed Oct 4 16:06:58 EDT 2000


in article YPLC5.167$YU1.7871 at newsc.telia.net, Fredrik Lundh wrote::

> Stephen Hansen wrote:
>> Okay, say I have three different strings:
>> #1: they
>> #2: that
>> #3: tommy
>> 
>> And a user gives me a string -- 'the', I want it to match to 'they'. Then
>> say they give me a string, 'to', I want it to match to 'tommy'. A string
>> of 'th' or 't' is ambiguious, and i want a list returned, ['they','that']
>> and ['they','that','tommy'] respectively.
> 
> import re, string
> 
> class Matcher:
>   def __init__(self, names):
>       self.names = string.join(map(lambda x: "{%s}" % x, names), "")
>   def find(self, name):
>       return re.findall("{(" + re.escape(name) + "[^}]*)}", self.names)

All I can say is wow.




More information about the Python-list mailing list