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

I need to remove every instance of a double-blank from a string leaving intact all original single spaced text.

I tried

my $string = "hi there world! La la 12 12 14 : 19 : hi"; $string =~ s/\s{2}//g;
But it fails and appears to remove nothing. Any pointers?

Replies are listed 'Best First'.
Re: removing 2 blanks from a string
by Fletch (Bishop) on Jun 20, 2006 at 17:21 UTC

    Erm . . .

    $ perl <<'EOT' heredoc> my $string = "hi there world! La la 12 12 14 : 19 : hi +"; heredoc> heredoc> $string =~ s/\s{2}//g; heredoc> print $string, "\n"; heredoc> EOT hi there world! Lala12 1214:19:hi

    Looks like it's removed any occurrence of exactly 2 space characters to me.

Re: removing 2 blanks from a string
by sh1tn (Priest) on Jun 20, 2006 at 17:11 UTC
    $string =~ s/\s{2,}/ /g; # the space in your replacement is missing


Re: removing 2 blanks from a string
by swampyankee (Parson) on Jun 20, 2006 at 17:23 UTC

    This sounds like a job for split()!

    $string = join(' ', split(/ +/,$string));

    May work. Usually, I would split on white space, vs blanks, so I would usually do something like

    $string = join(' ', split(/\s+/,$string));

    If this is part of a more general text processing issue, I'd suggest taking a look at Text::Wrap or Text::Autoformat

    emc

    e(π√−1) = −1
Re: removing 2 blanks from a string
by rsriram (Hermit) on Jun 26, 2006 at 12:47 UTC

    Try this,
    $string =~ s/( )+/ /g;