substr vs regex

Aaron Crane perl at
Mon Sep 3 11:10:25 BST 2007

alex at writes:
> Imagine, say, someone wanted the last three characters of a
> string. They might use a regex /(...)$/ or substr($variable, -3)

Note that those aren't actually equivalent:

  $ cat
  sub show {
     my ($s) = @_;
     return "undef\n" if !defined $s;
     $s =~ s/\n/\\n/g;
     return ">>$s<<\n";
  for ("foobar\n", "foobar\n\n") {
      my ($via_regex) = /(...)$/;
      my ($via_substr) = substr $_, -3;
      print "\ntarget:     ", show($_);
      print "via regex:  ", show($via_regex);
      print "via substr: ", show($via_substr);
  $ perl

  target:     >>foobar\n<<
  via regex:  >>bar<<
  via substr: >>ar\n<<

  target:     >>foobar\n\n<<
  via regex:  undef
  via substr: >>r\n\n<<

You can make them equivalent by adding the /m flag to the regex, and
using /\z/ instead of /$/.

> Is one of these significantly more efficient than the other?

I wouldn't worry about the efficiency anywhere near as much as the
readability.  And it's hard to say which is more readable without more
context about what your program's doing.

Aaron Crane

More information about the mailing list