Quarter Life Crisis

The world according to Sven-S. Porst

« Party like you’re 27MainDie Entdeckung der Langsamkeit »

Parsing Question

107 words

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?

September 14, 2004, 11:45


Comment by tom: User icon

can there be apostrophes in the values?

September 14, 2004, 12:50

Comment by ssp: User icon

I looks like this doesn’t happen. (They seem to be converted to spaces beforehand.)

September 14, 2004, 13:52

Comment by Tom: User icon

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);

warn Dumper(\%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.

September 14, 2004, 14:16

Comment by Tom: User icon

Bah. You need comment previewing. Hope this is better.

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.

s/'//g for values(%output);

warn Dumper(\%output);

September 14, 2004, 14:18

Comment by Tom: User icon

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.

September 14, 2004, 14:26

Comment by Torsten: User icon

s/ ((‘[^’]’)|[^’= ])=((‘[^’]’)|[^’= ]*) +/$1=$3$separator/g;

Like this?

September 14, 2004, 18:22

Comment by ssp: User icon

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…

September 17, 2004, 16:23

Add your comment

« Party like you're 27MainDie Entdeckung der Langsamkeit »

Comments on




This page

Out & About

pinboard Links


Received data seems to be invalid. The wanted file does probably not exist or the guys at last.fm changed something.