in reply to How to get only the next word matching the regex in the line
.* is almost never useful in a well constructed regex. Instead use a character set restricted to just the allowed characters, or a negated character set that lists characters that are not allowed. In your case the closing ' character is a good candidate for a negated character set:
use strict; use warnings; my @grabArray; while (my $current_line = <DATA>) { next if $current_line !~ /Grab this\s+'([^']+)'/; push @grabArray, $1; } print join "\n", @grabArray; __DATA__ Grab this 'test1' (repeat/new) many lines Grab this 'test2' (repeat/new) many lines Grab this 'test3' (repeat/new)
Prints:
test1 test2 test3
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to get only the next word matching the regex in the line
by AnomalousMonk (Archbishop) on Nov 11, 2019 at 04:11 UTC | |
by oysterperl (Novice) on Nov 11, 2019 at 05:29 UTC |