http://qs321.pair.com?node_id=241023

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys
I'm having a problem with the following regex
$line =~ s/.{2}\s+/:/;
All I want to do is swap any number of spaces (the first 2) with ":"
i.e.
$line = 'this has three spaces'; #before $line = 'this:has:three spaces'; #after
Any idea what I'm doing wrong?

Replies are listed 'Best First'.
Re: simple regex problem
by Enlil (Parson) on Mar 06, 2003 at 23:07 UTC
    First the regex that does what you want (as most regexes assuming a lot):
    $line =~ s/\s+(\S+)\s+/:$1:/;
    which will replace the first instance of
    "a series of whitespace followed by a series non-whitespace followed by a series of whitespace",
    with a
    "colon, followed by the set of non-whitespace matched, followed by another colon".

    You will have to modify this further to get the result you posted as it is also removing the set of spaces after "three" and replacing it with one space.

    Any idea what I'm doing wrong?

    Your code can be broken down to a pattern like so:
    . will match any character besides \n. The {2} at the end means do this twice. This has to be followed by
    \s+ which means the pattern has to have at least one whitespace character but will capture as many of these in a row as it can.
    When your pattern matches the first time in the line it will replace it with a colon.

    As per usual,let me direct you to perlre for your regex needs.

    -enlil

Re: simple regex problem
by CukiMnstr (Deacon) on Mar 06, 2003 at 23:13 UTC
    All I want to do is swap any number of spaces (the first 2) with ":"

    you want to replace the first two ocurrences of one or more spaces with ":"? and then replace any other more-than-one spaces (damn, my english sucks today!) with one space?

    $line =~ s/\s+/ /g; # replace more than one space with one space $line =~ s/ /:/ for (1, 2); # replace the first two spcs with a colon #- does not work if there's only one space - $line =~ s/ (\S+) /:$1:/;
    couldn't think of a way to do it with a single substitution...

    update. fixed bug when there is only one space. thanks blakem!

    hope it helps,

      I like this approach (its the only one listed that fits the "only two" spec) but what happens if there is only one space?
      "this    hasonlyonespace" => "this hasonlyonespace"

      How about:

      $line =~ s/\s+/ /g; $line =~ s/ /:/ for (1,2);

      -Blake

      $line =~ s/\s+/ /g;     # replace more than one space with one space

      This replaces a sequence of whitespaces with a single space char. This might or might not be what the poster wanted. To just affect spaces and replaces multiple occurrences with one, I'd recommend tr///: $line =~ tr/ / /s;

      -- Hofmator

Re: simple regex problem
by allolex (Curate) on Mar 06, 2003 at 22:55 UTC
    s/\s+/:/g
    or
    s/[ ]+/:/g
    \s = space-like things, like tabs
    + = one or more
    [ ] = just a normal space

    You might want to have a look at perldoc perlre. What you have matches anything (".") twice and then one or more spaces, replacing them all with a colon.

    --
    Allolex

    Update: I updated this page a whole lot of times, adding more and more info each time. Silly, isn't it?