Robust regex

Chris Angelico rosuav at gmail.com
Mon Nov 19 15:42:19 EST 2012


On Tue, Nov 20, 2012 at 7:32 AM, Joseph L. Casale
<jcasale at activenetwerx.com> 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!

Is regex a requirement? Since you posted this on python-list, I'm
going to assume you're working in Python.

string = 'key_1|||value_1||||key_2|||value_2'
content = dict(map(lambda x: x.split("|||"),string.split("||||")))

--> {'key_1': 'value_1', 'key_2': 'value_2'}

ChrisA



More information about the Python-list mailing list