in reply to Manipulating the Capture(s) of Regular Expressions

You can use @- and @+ to do this...see @-.
if ( $text =~ m/cow\s+(jumped)\s+over/ ) { substr($text, $-[1], $+[1] - $-[1], "tripped"); }

Replies are listed 'Best First'.
Re^2: Manipulating the Capture(s) of Regular Expressions
by ikegami (Patriarch) on Jan 12, 2009 at 05:18 UTC
    And by extension,
    sub transform { my ($ref) = @_; $$ref = 'tripped'; } $text = 'The brown cow jumped over the moon'; if ( $text =~ m/cow\s+(jumped)\s+over/ ) { my $ref = \substr($text, $-[1], $+[1] - $-[1]); transform($ref); } print("$text\n"); # The brown cow tripped over the moon

    Update: Expanded the example to be runnable.

      You two just shot to the top of my mental 'monks who really know Perl and should be listened to' list... :)

      (Edit: well, maybe just after TimToady... :)


      Life is denied by lack of attention,
      whether it be to cleaning windows
      or trying to write a masterpiece...
      -- Nadia Boulanger
Re^2: Manipulating the Capture(s) of Regular Expressions
by monarch (Priest) on Jan 12, 2009 at 05:46 UTC
    Wow; this is perfect, and I thought I'd read the Camel book (I was wrong). I just looked through it again now (3rd edition), and indeed it's documented in the Special Variables section.