[Tutor] Making Regular Expressions readable

member thudfoo thudfoo at opensuse.us
Mon Mar 8 18:59:28 CET 2010


On Mon, Mar 8, 2010 at 9:34 AM, spir <denis.spir at gmail.com> wrote:
> On Mon, 8 Mar 2010 16:12:35 +0000
> Stephen Nelson-Smith <sanelson at gmail.com> wrote:
>
>> Hi,
>>
>> I've written this today:
>>
>> #!/usr/bin/env python
>> import re
>>
>> pattern = r'(?P<ForwardedFor>^(-|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(,
>> [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})*){1})
>> (?P<RemoteLogname>(\S*)) (?P<RemoteUser>(\S*))
>> (?P<Timestamp>(\[[^\]]+\]))
>> (?P<FirstLineOfRequest>(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)
>> (?P<Status>(\S*)) (?P<Size>(\S*))
>> (?P<Referrer>(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)
>> (?P<UserAgent>(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)(
>> )?(?P<SiteIntelligenceCookie>(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)'
> [...]
>> It works fine, but it looks pretty unreadable and unmaintainable to
>> anyone who hasn't spent all day writing regular expressions.

You might want to consider Plex:
www.cosc.canterbury.ac.nz/greg.ewing/python/Plex/


More information about the Tutor mailing list