in reply to Re: Regex operation optimisation
in thread Regex operation optimisation

Odd. I would have done

- replace continuous white spaces with a single underscore

$string =~ s/\s+/\_/g;

The question I have; is it simple preference or is there a technical reason you use the /s approach?

I sense an opportunity for me to learn something, Obi Wan .

Replies are listed 'Best First'.
Re^3: Regex operation optimisation
by jwkrahn (Abbot) on Jul 02, 2010 at 23:36 UTC

    perlpal's example was $string =~ s/ {1,}/_/g; which matches a space character while your example $string =~ s/\s+/\_/g; uses the whitespace character class (\s) which matches "\t", "\r", "\n" and "\f" as well as the space character.    In general, the tr/// operator is more efficient then the s/// operator so I try to use it when appropriate.

      Thank you!

      I was right, this WAS an opportunity for me to learn something.

      :: giggle ::

      Love days like that.