Regular expression for "key = value" pairs

Discussion in 'Python' started by Ciccio, Dec 22, 2010.

  1. Ciccio

    Ciccio Guest

    Hi all,
    suppose I have:

    s='a=b, c=d'

    and I want to extract sub-strings a,b,c and d from s (and in general
    from any longer list of such comma separated pairs).
    Some failed attempts:

    In [12]: re.findall(r'(.+)=(.+)', s)
    Out[12]: [('a=b, c', 'd')]

    In [13]: re.findall(r'(.+?)=(.+)', s)
    Out[13]: [('a', 'b, c=d')]

    In [14]: re.findall(r'(.+)=(.+)*', s)
    Out[14]: [('a=b, c', 'd')]

    In [15]: re.findall(r'(.+)=(.+),', s)
    Out[15]: [('a', 'b')]

    In [16]: re.findall(r'(.+)=(.+),?', s)
    Out[16]: [('a=b, c', 'd')]

    Thanks for your help,
    francesco.
    Ciccio, Dec 22, 2010
    #1
    1. Advertising

  2. Ciccio

    Mark Wooding Guest

    Ciccio <> writes:

    > suppose I have:
    >
    > s='a=b, c=d'
    >
    > and I want to extract sub-strings a,b,c and d from s (and in general
    > from any longer list of such comma separated pairs).

    [...]
    > In [12]: re.findall(r'(.+)=(.+)', s)
    > Out[12]: [('a=b, c', 'd')]


    I think there are two logically separate jobs here: firstly, extracting
    the comma-separated pairs, and secondly parsing the individual pairs.

    If you want the extra problem of dealing with regular expressions, this
    seems to be the way to do it.

    R_PAIR = re.compile(r'''
    ^\s*
    ([^=\s]|[^=\s][^=]*[^=\s])
    \s*=\s*
    (\S|\S.*\S)
    \s*$
    ''', re.X)

    def parse_pair(pair):
    m = R_PAIR.match(pair)
    if not m:
    raise ValueError, 'not a `KEY = VALUE\' pair'
    return m.groups([1, 2])

    The former is even easier.

    R_COMMA = re.compile(r'\s*,\s*')

    kvs = [parse_pair(p) for p in R_COMMA.split(string)]

    Apply gold-plating to taste.

    But actually, it's much easier to avoid messing with regular expressions
    at all.

    def parse_pair(pair):
    eq = pair.index('=')
    return pair[:eq].strip(), pair[eq + 1:].strip()

    kvs = [parse_pair(p) for p in string.split(',')]

    -- [mdw]
    Mark Wooding, Dec 22, 2010
    #2
    1. Advertising

  3. Ciccio

    Ciccio Guest

    I extracted an isolated problem from a slightly more complex
    situation, that's why I'm using REs.
    Thank you all for your help, my problem is now solved.
    Ciccio, Dec 23, 2010
    #3
    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. VSK
    Replies:
    2
    Views:
    2,267
  2. heromull
    Replies:
    1
    Views:
    372
    Malik Asif Joyia
    Feb 24, 2005
  3. He Shiming
    Replies:
    3
    Views:
    3,813
    He Shiming
    Dec 26, 2004
  4. Markus Dehmann

    key-value pairs: key consists of 3 ints

    Markus Dehmann, Jan 15, 2006, in forum: C++
    Replies:
    13
    Views:
    623
    Richard Herring
    Jan 23, 2006
  5. Antonio Quinonez
    Replies:
    2
    Views:
    153
    Antonio Quinonez
    Aug 14, 2003
Loading...

Share This Page