Help: Creating condensed expressions

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Fri Mar 24 17:38:04 EST 2006


David Hirschfield a écrit :
> Here's the problem: Given a list of item names like:
> 
> apple1
> apple2
> apple3_SD
> formA
> formB
> formC
> kla_MM
> kla_MB
> kca_MM
> 
> which is a subset of a much larger list of items,
> is there an efficient algorithm to create condensed forms that match 
> those items, and only those items? Such as:
> 
> apple[12]
> apple3_SD
> form[ABC]
> kla_M[MB]
> kca_MM



> The condensed expression syntax only has [...] and * as operators. [...] 
> matches a set of individual characters, * matches any string.
> I'd be satisfied with a solution that only uses the [...] syntax, since 
> I don't think it's possible to use * without potentially matching items 
> not explicitly in the given list.
> 
> I'm not sure what this condensed expression syntax is called (looks a 
> bit like shell name expansion syntax),

Looks like a very restricted subset of regular expressions.

> and I'm not even sure there is an 
> efficient way to do what I'm asking. Any ideas would be appreciated.


import re

lines = """
apple1
apple2
apple3_SD
formA
formB
formC
kla_MM
kla_MB
kca_MM
""".strip().split()

patterns = [
     r'apple[12]',
     r'apple3_SD',
     r'form[ABC]',
     r'kla_M[MB]',
     r'kca_MM',
     ]

for pat in patterns:
     for line in lines:
         m = re.match(pat, line)
         print "%s match %s : %s" % (pat, line, m and "Yes" or 'No')


HTH



More information about the Python-list mailing list