Making regex suck less

John La Rooy igetenoughspamalreadythanksjlr at doctor.com
Sun Sep 1 18:21:32 EDT 2002


Carl Banks wrote:
> Gerhard H?ring wrote:
> 
>>>which means the real time is not spent in the compile() function, but
>>>in the match or find function. So basically, couldn't one come up with
>>>a *human readable* syntax for re, and compile that instead?
>>
>>That's equally powerful? Most probably not.
> 
> 
> Why not?  It won't be as fast, but it should be able to do anything a
> regexp can do, and would be much more versatile.
> 
>
I think the main problem is that *human readable* doesn't map really 
well onto regular expressions.

What would the equivalent of r"(.)(.)(.)\3\2\1"
This means a "palindrome of 6 characters"
But it is unlikely that the human readable processor would understand 
that (isn't it??)

It would be more likely to look like this (I haven't put too much 
thought into this)
"anything,anything,anything,same_as_3rd,same_as_2nd,same_as_1st"
or would you like to suggest something else?

palindrome_6 = re.compile(r"(.)(.)(.)\3\2\1")
palindrome_6 = 
re.compile("anything,anything,anything,same_as_3rd,same_as_2nd,same_as_1st")

Sure there are some cases where the re is loaded with meta characters...

hmmm
OK is this about writing maintainable code or people not wanting to 
learn all the ins and outs of re's?

John




More information about the Python-list mailing list