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

So out of curiosity, what would be the smallest regex alternative of the following code:
$string = "hello world"; print substr(string, 6,5);

Replies are listed 'Best First'.
Re: substr regex alternative
by JavaFan (Canon) on Apr 24, 2012 at 22:16 UTC
    print$string=~/world/g; # Or print$string=~/w.{4}/g;
Re: substr regex alternative
by 2teez (Vicar) on Apr 24, 2012 at 22:20 UTC
    my $string = "hello world"; print $string =~ /(.{5})$/; #Or print $string =~ /.{5}$/g;

      With the thin requirements, maybe even go a bit smaller.

      print$string=~/\w+$/g
Re: substr regex alternative
by jwkrahn (Abbot) on Apr 24, 2012 at 21:49 UTC
    $string = "hello world"; print $string =~ /^.{6}(.{5})/;
Re: substr regex alternative
by trizen (Hermit) on Apr 24, 2012 at 22:16 UTC
    print $string =~ /^.{6}(.{1,5})/s;
Re: substr regex alternative
by BillKSmith (Monsignor) on Apr 25, 2012 at 03:32 UTC
    The variety of responses so far reflect the poor requirements. Did you mean "smallest" in the "perl golf" sense? Did you want a alternative for all the code, or just the print statement?
      i'm looking for an obfuscated alternative, or perl golf i guess
Re: substr regex alternative
by GrandFather (Saint) on Apr 25, 2012 at 21:12 UTC

    The smallest alternative is:

    Under strictures one variant is:

    x

    With the typo/trick question "corrected" an interesting alternative is:

    $string =~ s/(\w+)$/print $1/e;
    my $string = "hello world"; $string=~s/(\w+)$/print$1/e; </c>
    True laziness is hard work