Re: One liners

Discussion in 'Python' started by Roy Smith, Dec 7, 2013.

  1. Roy Smith

    Roy Smith Guest

    In article <>,
    Joel Goldstick <> wrote:

    > Aside from django urls, I am not sure I ever wrote regexes in python. For
    > some reason they must seem awfully sexy to quite a few people. Back to my
    > point above -- ever try to figure out a complicated regex written by
    > someone else?


    Regex has a bad rap in the Python community. To be sure, you can abuse
    them, and write horrible monstrosities. On the other hand, stuff like
    this (slightly reformatted for posting):

    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}) '
    r'\[(?P<accept_date>\d{2}/\w{3}/\d{4}:)\d{2}){3}\.\d{3})] '
    r'(?P<frontend_name>\S+) '
    r'(?P<backend_name>\S+)/'
    r'(?P<server_name>\S+) '
    r'(?P<Tq>(-1|\d+))/'
    r'(?P<Tw>(-1|\d+))/'
    r'(?P<Tc>(-1|\d+))/'
    r'(?P<Tr>(-1|\d+))/'
    r'(?P<Tt>\+?\d+) '
    r'(?P<status_code>\d{3}) '
    r'(?P<bytes_read>\d+) '
    r'(?P<captured_request_cookie>\S+) '
    r'(?P<captured_response_cookie>\S+) '
    r'(?P<termination_state>[\w-]{4}) '
    r'(?P<actconn>\d+)/'
    r'(?P<feconn>\d+)/'
    r'(?P<beconn>\d+)/'
    r'(?P<srv_conn>\d+)/'
    r'(?P<retries>\d+) '
    r'(?P<srv_queue>\d+)/'
    r'(?P<backend_queue>\d+) '
    r'(\{(?P<request_id>.*?)\} )?' # Comment out for stock haproxy
    r'(\{(?P<captured_request_headers>.*?)\} )?'
    r'(\{(?P<captured_response_headers>.*?)\} )?'
    r'"(?P<http_request>.+)"'
    )

    while intimidating at first glance, really isn't that hard to
    understand. Python's raw string literals, adjacent string literal
    catenation, and automatic line continuation team up to eliminate a lot
    of extra fluff.
     
    Roy Smith, Dec 7, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Curly Joe

    Re: Clever One Liners

    Curly Joe, Jul 3, 2003, in forum: Python
    Replies:
    0
    Views:
    496
    Curly Joe
    Jul 3, 2003
  2. holger krekel

    Re: Clever One Liners

    holger krekel, Jul 3, 2003, in forum: Python
    Replies:
    0
    Views:
    844
    holger krekel
    Jul 3, 2003
  3. vj
    Replies:
    0
    Views:
    297
  4. kj
    Replies:
    2
    Views:
    419
    unayok
    Jun 18, 2009
  5. Larry
    Replies:
    1
    Views:
    106
    Martien Verbruggen
    Feb 3, 2005
Loading...

Share This Page