1

I'm new to both perl and using regex. I need to remove the white space from a string.

I found an example but its pretty opaque to me. Is this an accurate description of whats happening?

sub trim($)
{
  my $string = shift;
  $string =~ s/^\s+//;
  # =~         : regex on variable string
  # s/         : replace match with value
  # ^\s+       : one or more white space characters at beginning
  # //         : no characters

  $string =~ s/\s+$//;
  #  =~        : regex on variable $string
  # s/         : replace match with value
  # \s+$       : one or more white space characters until end of line
  # //         : no characters
  return $string;
}
flakes
  • 21,558
  • 8
  • 41
  • 88
  • 3
    `=~` and `s///` are documented in perlop [here](http://perldoc.perl.org/perlop.html#Binding-Operators) and [here](http://perldoc.perl.org/perlop.html#s%2f_PATTERN_%2f_REPLACEMENT_%2fmsixpodualngcer). If you have perldoc installed on your system, you can even view the documentation locally by running `perldoc perlop`. [perlretut](http://perldoc.perl.org/perlretut.html) and [perlre](http://perldoc.perl.org/perlre.html) are good resources for understanding regular expressions. – ThisSuitIsBlackNot Mar 08 '16 at 20:08
  • 5
    Also, you should generally [avoid prototypes](http://stackoverflow.com/questions/297034/why-are-perl-5s-function-prototypes-bad) in your own code. Instead of `sub trim($) { ... }` do `sub trim { ... }` – ThisSuitIsBlackNot Mar 08 '16 at 20:10

2 Answers2

5

Yes it is.

Nothing else to say, actually.

sidyll
  • 57,726
  • 14
  • 108
  • 151
1

Yes it is, as answered by sidyll. All your comments are accurate. Since these are basics you are asking, I would like to add a little.

You can do both in one expression, s/^\s+|\s+$//g (there are slight efficiency considerations). Note that now you need /g ("global") modifier so that all \s+ are found. Otherwise the engine stops after it finds ^\s+ (if there are any) and you are left with trailing space (if any).

You can use spaces in your regex, for readability, by using /x modifier. In this case it isn't much but with more complex ones it can help a lot.

$string =~ s% ^\s+ | \s+$ %%gx;

You may use different delimiters -- as long as you don't use that inside the regex. I use % above to avoid the editor coloring everything red (I find % not very readable in fact, but I need | inside). This is sometimes very useful, for example when your regex has a lot of /. Then you can use a different delimiter so you don't have to escape them.


Complete resources are given by ThisSuitIsBlackNot in the comment.

I've seen people praise this a lot: regex Demo, where you can type in a regex and see how it works.

Community
  • 1
  • 1
zdim
  • 64,580
  • 5
  • 52
  • 81