Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33555
| From | John Gordon <gordon@panix.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Robust regex |
| Date | 2012-11-19 20:50 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <k8e622$p08$1@reader1.panix.com> (permalink) |
| References | <mailman.7.1353357285.29569.python-list@python.org> |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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