Regular expressions

Peter Otten __peter__ at web.de
Sun Oct 26 05:46:08 EST 2003


Kill Bill wrote:

> That's not what I want.  If I have yipee as "kdasfjh", then I want to
> compare to all combinations, so that there will be a match when it
> compares it with say when line is "fas" because line has all those letters
> in yipee. But "fass" will not match because yipee does not have two "s"'s.
> 
> I'm new to python but I'm finding these regular expressions quite
> confusing.

Regexes predate Python, so don't blame it on the snake :-)

Here's an approach that does not use regular expressions. Perhaps you can
extend it to something useful:

class Matcher:
    def __init__(self, s):
        self.search = list(s)
        self.search.sort()
    def __call__(self, s):
        if len(s) != len(self.search):
            return False
        t = list(s)
        t.sort()
        return t == self.search

m = Matcher("peter")

for s in "peter retep treep preet".split():
    assert m(s)
    print s, "->", m(s)

for s in "netto pete trppe Preet".split():
    assert not m(s)
    print s, "->", m(s)

However, it has the overhead of string to list conversion.
Another drawback is that you have to split your input into chunks before
feeding it to the matcher. Depending on your problem these might overlap
and make the above simplistic matcher very inefficient.

But if I'm reading you correctly you always want to match the complete line:

m = Matcher("whatever")
for line in file("source"):
    if m(line.strip()):
        print line.strip()


Peter




More information about the Python-list mailing list