Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Robust regex (http://www.velocityreviews.com/forums/t954637-re-robust-regex.html)

MRAB 11-19-2012 08:50 PM

Re: Robust regex
 
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))



All times are GMT. The time now is 05:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.