I suppose the following is an easy task for someone who uses perl regularly or regular expression or similar stuff. On the other hand, it’ll cause a lot of headache to me. So perhaps someone proficient at these techniques could tell me the relevant line of script to do the following:
I have a bunch of keys and values of the form
key=value. Those are concatenated by spaces. And I’d like to replace those spaces by something else. The tricky bit is that there may be spaces in the values, in which case the whole value is enclosed in apostrophes ‘.
This should be easy, right?
can there be apostrophes in the values?
I looks like this doesn’t happen. (They seem to be converted to spaces beforehand.)
Ok, I think this will work in the general case.
#!/usr/bin/perl use warnings; use strict; use Data::Dumper;
my $string=” one=val two=’val’ three=’val with spaces’ “;
my (%output) = $string =~ m/ ([^\s=]+) # matches key = # ‘=’ ( # either.. ‘[^’]+’ # something in ’ | # or.. \S+ # a string of non-space chars ) # we’re done. /gx;
s/’//g for values(%output);
I’m putting the output into a hash - this assumes the keys are unique. Put it into a list and parse it yourself if this isn’t the case. The only annoyance is that I have to do a second step to strip the surrounding “’” chars - you could probably put this into the regexp, but I like the simplicity of the current one, and it’s easier to understand this way anyway.
Bah. You need comment previewing. Hope this is better.
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $string=" one=val two='val' three='val with spaces' "; my (%output) = $string =~ m/ ([^\s=]+) # matches key = # '=' ( # either.. '[^']+' # something in ' | # or.. \S+ # a string of non-space chars ) # we're done. /gx; s/'//g for values(%output); warn Dumper(\%output);
I’m going to mention http://dev.jerakeen.org/svn/tomi/Projects/Old/RegExpTest/ which is a little toy I use to play with regexpts. Needs Camelbones ( http://camelbones.sf.net ) and it’s a little strange to use - put a regexp in the top, text in the bottom, press Command-E, and everything that gets matched will be hilighted red. It’ll also print all the matches to the console / run log.
s/ ((‘[^’]’)|[^’= ])=((‘[^’]’)|[^’= ]*) +/$1=$3$separator/g;
Thanks guys. Let’s hope I’ll manage to use this properly.
Thanks for commenting the perl code. Otherwise I wouldn’t have understood it. Perl is even worse than C…
Received data seems to be invalid. The wanted file does probably not exist or the guys at last.fm changed something.