in reply to regex for string

I don't know why you want a regex for that, but here you go:
use strict; use warnings; my $str = "hello\nworld"; my ($first, $last); $str =~ m/ (?{ $first = substr($str, 0, 1); $last = substr($str, -1) } +) /x; print "first: '$first'; last: '$last'\n";

However note the caveats of (?{ ... }) groups as documented in perlre.

Perl 6 projects - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: regex for string
by saranperl (Initiate) on Aug 18, 2009 at 08:57 UTC
    why we want to write this much code using "substr"? we can use like
    $f = substr($str,0,1) $l=substr($str,-1);
    what want to get first and last char by regex ? like that m/(.?)..etc/
      Because substr is the way to go, but you wanted a regex.

      So I gave you a regex, but still used substr.

      The real question is, why do you insist on using a regex? it's not a good idea, and far from being an ideal solution.

      Perl 6 projects - links to (nearly) everything that is Perl 6.