Re: Robust regex

Discussion in 'Python' started by MRAB, Nov 19, 2012.

  1. MRAB

    MRAB Guest

    On 2012-11-19 20:32, 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!
    >

    Do you need to use regex?

    It would be simpler to use the .split method:

    for pair in string.split("||||"):
    key, value = pair.split("|||")
    print("key is {!r}, value is {!r}".format(key, value))
    MRAB, 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,067
    msalters
    Jan 25, 2005
  2. Replies:
    3
    Views:
    730
    Reedick, Andrew
    Jul 1, 2008
  3. Replies:
    3
    Views:
    155
    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. Chris Angelico

    Re: Robust regex

    Chris Angelico, Nov 19, 2012, in forum: Python
    Replies:
    0
    Views:
    130
    Chris Angelico
    Nov 19, 2012
Loading...

Share This Page