On Thu, Apr 4, 2013 at 8:39 AM, Roy Smith <r...@panix.com> wrote: > In article <mailman.96.1365077619.3114.python-l...@python.org>, > Jason Swails <jason.swa...@gmail.com> wrote: > > > The only time I regularly break my rule is for regular expressions (at > some > > point I may embrace re.X to allow me to break those up, too). > > re.X is a pretty cool tool for making huge regexes readable. But, it > turns out that python's auto-continuation and string literal > concatenation rules are enough to let you get much the same effect. > Here's a regex we use to parse haproxy log files. This would be utter > line noise all run together. This way, it's almost readable :-) > > pattern = re.compile(r'haproxy\[(?P<pid>\d+)]: ' > r'(?P<client_ip>(\d{1,3}\.){3}\d{1,3}):' > r'(?P<client_port>\d{1,5}) ' >
For some reason that never occurred to me. I use this technique every other time I want to break up a long string, but never for regexes... Now I will. I was wary of using re.X since I sometimes use meaningful whitespace in my regexes, and I didn't want to have to figure out how to prevent them from being ignored... This is a much better solution. Thanks, Jason
-- http://mail.python.org/mailman/listinfo/python-list