Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Robust regex

Reply
Thread Tools

Re: Robust regex

 
 
MRAB
Guest
Posts: n/a
 
      11-19-2012
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))

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Robust regex Joseph L. Casale Python 2 11-19-2012 11:37 PM
Re: Robust regex Chris Angelico Python 0 11-19-2012 08:42 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
regex bug (comments within regex not as robust) kg.google@olympiakos.com Perl Misc 3 10-27-2005 07:21 PM
robust iterator implementation Martin Smith C++ 4 01-25-2005 11:12 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57