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


Groups > comp.lang.python > #33556

Re: Robust regex

Date 2012-11-19 20:50 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: Robust regex
References <assp.06702d7ae4.92097A6A775D5147B1078E3F15430B92348EA96E@prato.activenetwerx.local>
Newsgroups comp.lang.python
Message-ID <mailman.11.1353358434.29569.python-list@python.org> (permalink)

Show all headers | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web