[Python-de] Noch mal RegEx :-((

Gerson Kurz Gerson.Kurz at t-online.de
Die Jul 1 22:47:59 EDT 2003


Du könntest das selbst stricken. Realisiert wird folgende Syntax:

# INPUT = [SIGN] NUMBER [FRACTION] EOS
# SIGN = "-"
# NUMBER = DIGIT {DIGIT}
# FRACTION = "," NUMBER

class parser:
    def check(self, digits):
        self.k = 0
        self.digits = digits
        self.peek('-')
        return self.number() and self.fraction() and self.eos()

    def eos(self):
        return self.k == len(self.digits)

    def peek(self, c):
        if not self.eos() and self.digits[self.k:self.k+1] in c:
            self.k += 1
            return True

    def digit(self):
        return self.peek("0123456789")

    def fraction(self):
        return not self.peek(',') or self.number()

    def number(self):
        if self.digit():
            while self.digit():
                pass
            return True

if __name__ == "__main__":
    # testprogramm:

    for k in ("1234", "-1234", "1234,12", "-1234,12"):
        assert parser().check(k)

    for k in ("k123", "123k", "123,", "+123", "1234,,", "124k" ):
        assert not parser().check(k)