Re: Robust regex

Discussion in 'Python' started by Chris Angelico, Nov 19, 2012.

  1. On Tue, Nov 20, 2012 at 7:32 AM, Joseph L. Casale
    <> wrote:
    > Trying to robustly parse a string that will have key/value pairs separated
    > by three pipes, where each additional key/value (if more than one exists)
    > will be delineated by four more pipes.
    >
    > string = 'key_1|||value_1||||key_2|||value_2'
    > regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?'
    >
    > I am not convinced this is the most effective or safest, any opinions would
    > be greatly appreciated!


    Is regex a requirement? Since you posted this on python-list, I'm
    going to assume you're working in Python.

    string = 'key_1|||value_1||||key_2|||value_2'
    content = dict(map(lambda x: x.split("|||"),string.split("||||")))

    --> {'key_1': 'value_1', 'key_2': 'value_2'}

    ChrisA
    Chris Angelico, Nov 19, 2012
    #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. Martin Smith

    robust iterator implementation

    Martin Smith, Jan 24, 2005, in forum: C++
    Replies:
    4
    Views:
    4,068
    msalters
    Jan 25, 2005
  2. Replies:
    3
    Views:
    732
    Reedick, Andrew
    Jul 1, 2008
  3. Replies:
    3
    Views:
    157
    Paul Lalli
    Oct 27, 2005
  4. Joseph L. Casale

    Robust regex

    Joseph L. Casale, Nov 19, 2012, in forum: Python
    Replies:
    2
    Views:
    158
    Joseph L. Casale
    Nov 19, 2012
  5. MRAB

    Re: Robust regex

    MRAB, Nov 19, 2012, in forum: Python
    Replies:
    0
    Views:
    133
Loading...

Share This Page