Making regex suck less

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Tue Sep 3 13:42:27 EDT 2002


jepler at unpythonic.net <jepler at unpythonic.net> wrote:
>On Mon, Sep 02, 2002 at 09:23:18PM +1000, John La Rooy wrote:
>> Carl Banks wrote:
>> >
>> >pattern = Group(Any()) + Group(Any()) + Group(Any()) \
>> >          + GroupRef(3) + GroupRef(2) + GroupRef(1)
>> >
>> Err symantically that's exactly the same as the re and my suggestion
>> only the syntax is different. It's still nothing like saying
>> 
>> pattern = "6 character palindrome"
>
>Do you mean something like this?
>
>    def palindrome_re(n):
>        pat = ["(.)" * ((n+1)/2)]
>        for i in range(n/2, 0, -1):
>            pat.append("\\%d" % i)
>        return "".join(pat)
[snip]
>
>I think that building REs in functions is a great approach for more
>complex REs.

It would also be useful if patterns can be built up as structured objects:

    def palindrome_re(n):
        pat = Empty()
        for i in range(n):  pat += Group(Any())
        for i in range(n, 0, -1): pat += GroupRef(i)
        return pat

    class Palindrom:
        def __init__(self, n): self.n = n
        def __call__(self): return palindrome_re(self.n)

    pattern = Palindrom(6)

Once the tree structure is revealed it could be mapped to whatever syntax
that is convenient for the situation.  The perl-like syntax would just be
one of them.

Huaiyu



More information about the Python-list mailing list