Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Robust regex

Reply
Thread Tools

Robust regex

 
 
Joseph L. Casale
Guest
Posts: n/a
 
      11-19-2012
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!

jlc
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      11-19-2012
In <mailman.7.1353357285.29569.python-> "Joseph L. Casale" <> writes:

> 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!


Regexes may be overkill here. A simple string split might be better:

string = 'key_1|||value_1||||key_2|||value_2'
pairs = string.split('||||')
for pair in pairs:
keyval = pair.split('|||')
print '%s=%s' % (keyval[0], keyval[1])

--
John Gordon A is for Amy, who fell down the stairs
B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
Joseph L. Casale
Guest
Posts: n/a
 
      11-19-2012
> Regexes may be overkill here. A simple string split might be better:

Yup, and much more robust as I was looking for.

Thanks everyone!
jlc
 
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
Re: Robust regex MRAB Python 0 11-19-2012 08:50 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