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


Groups > comp.lang.python > #33556 > unrolled thread

Re: Robust regex

Started byMRAB <python@mrabarnett.plus.com>
First post2012-11-19 20:50 +0000
Last post2012-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.


Contents

  Re: Robust regex MRAB <python@mrabarnett.plus.com> - 2012-11-19 20:50 +0000

#33556 — Re: Robust regex

FromMRAB <python@mrabarnett.plus.com>
Date2012-11-19 20:50 +0000
SubjectRe: 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))

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web