Robust regex

John Gordon gordon at panix.com
Mon Nov 19 21:50:10 CET 2012


In <mailman.7.1353357285.29569.python-list at python.org> "Joseph L. Casale" <jcasale at activenetwerx.com> writes:

> 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!

Regexes may be overkill here.  A simple string split might be better:

    string = 'key_1|||value_1||||key_2|||value_2'
    pairs = string.split('||||')
    for pair in pairs:
        keyval = pair.split('|||')
        print '%s=%s' % (keyval[0], keyval[1])

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"



More information about the Python-list mailing list