use a regex or not?
Joost Jacob
Joost.Jacob at gmail.com
Tue Jun 21 10:18:15 EDT 2005
I am looking for a function that takes an input string
and a pattern, and outputs a dictionary.
# @param s str, lowercase letters
# @param p str, lowercase and uppercase letters
# @return dict
def fill(s, p):
d = {}
....
return d
String s has characters from the lowercase letters.
String p is a pattern, a string of characters from the
lowercase and uppercase letters. The idea is to match
s with p, where lowercase letters have to match
exactly, and to fill variables (with an uppercase
letter name) with the rest of s. The variables are
collected in a dictionary with the resulting bindings.
A variable that occurs in more than one place in p must
bind to the same substring of s.
Tests:
>>> fill('ab', p='aA')
{'A': 'b'}
>>> fill('ab', p='Ab')
{'A': 'a'}
>>> fill('bb', p='Ab') # no match
{}
>>> fill('aa', p='Aa')
{'A': 'a'}
>>> fill('aa', p='Ab') # no match
{}
>>> fill('abb', p='aA')
{'A': 'bb'}
>>> fill('aba', p='aAa')
{'A': 'b'}
>>> fill('abb', p='aAa') # no match
{}
>>> fill('abab', p='aAaA') # A-matches must be equal
{'A': 'b'}
>>> fill('abac', p='aAaA') # no match
{}
>>> fill('abac', p='aAaB')
{'A': 'b', 'B': 'c'}
Can you do it? Is trying a solution with a regex a
good idea?
More information about the Python-list
mailing list