Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33555

Re: Robust regex

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news.linkpendium.com!news.linkpendium.com!panix!gordon
From John Gordon <gordon@panix.com>
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 <k8e622$p08$1@reader1.panix.com> (permalink)
References <mailman.7.1353357285.29569.python-list@python.org>
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

Show key headers only | View raw


In <mailman.7.1353357285.29569.python-list@python.org> "Joseph L. Casale" <jcasale@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@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Robust regex "Joseph L. Casale" <jcasale@activenetwerx.com> - 2012-11-19 20:32 +0000
  Re: Robust regex John Gordon <gordon@panix.com> - 2012-11-19 20:50 +0000
    RE: Robust regex "Joseph L. Casale" <jcasale@activenetwerx.com> - 2012-11-19 23:37 +0000

csiph-web