Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33551 > unrolled thread
| Started by | "Joseph L. Casale" <jcasale@activenetwerx.com> |
|---|---|
| First post | 2012-11-19 20:32 +0000 |
| Last post | 2012-11-19 23:37 +0000 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | "Joseph L. Casale" <jcasale@activenetwerx.com> |
|---|---|
| Date | 2012-11-19 20:32 +0000 |
| Subject | Robust regex |
| Message-ID | <mailman.7.1353357285.29569.python-list@python.org> |
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!
jlc
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2012-11-19 20:50 +0000 |
| Message-ID | <k8e622$p08$1@reader1.panix.com> |
| In reply to | #33551 |
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"
[toc] | [prev] | [next] | [standalone]
| From | "Joseph L. Casale" <jcasale@activenetwerx.com> |
|---|---|
| Date | 2012-11-19 23:37 +0000 |
| Message-ID | <mailman.19.1353368359.29569.python-list@python.org> |
| In reply to | #33555 |
> Regexes may be overkill here. A simple string split might be better: Yup, and much more robust as I was looking for. Thanks everyone! jlc
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web