Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news.linkpendium.com!news.linkpendium.com!panix!gordon From: John Gordon Newsgroups: comp.lang.python Subject: Re: Robust regex Date: Mon, 19 Nov 2012 20:50:10 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 23 Message-ID: References: NNTP-Posting-Host: panix1.panix.com X-Trace: reader1.panix.com 1353358210 25608 166.84.1.1 (19 Nov 2012 20:50:10 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Mon, 19 Nov 2012 20:50:10 +0000 (UTC) User-Agent: nn/6.7.3 Xref: csiph.com comp.lang.python:33555 In "Joseph L. Casale" 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@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies"