in reply to return a word next to the word you give
Another possible variant, say, in case there are multiple color attributes on the line, you want to capture all of them in an array.use strict; my @file = <STDIN>; my $word = "color:"; # no space after colon for my $lineno ( 0 .. $#file ) { if ($file[$lineno] =~ /\b$word\s*(\w+)/){ # notice the capture bra +ckets my $value = $1; # $value here is 'red'. ... } }
my @colors = $file[$lineno] =~ /\b$word\s*(\w+)/g;
|
|---|