Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33556 > unrolled thread
| Started by | MRAB <python@mrabarnett.plus.com> |
|---|---|
| First post | 2012-11-19 20:50 +0000 |
| Last post | 2012-11-19 20:50 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Robust regex MRAB <python@mrabarnett.plus.com> - 2012-11-19 20:50 +0000
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-11-19 20:50 +0000 |
| Subject | Re: Robust regex |
| Message-ID | <mailman.11.1353358434.29569.python-list@python.org> |
On 2012-11-19 20:32, Joseph L. Casale wrote:
> 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!
>
Do you need to use regex?
It would be simpler to use the .split method:
for pair in string.split("||||"):
key, value = pair.split("|||")
print("key is {!r}, value is {!r}".format(key, value))
Back to top | Article view | comp.lang.python
csiph-web