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
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

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

    next if $current_line !~ /Grab this\s+'([^']+)'/;
    oysterperl:   Note that  '([^']+)' will not correctly match a single-quoted string that contains an escaped single-quote, but if that can occur, a regex can easily be written that can handle an escaped delimiter.


    Give a man a fish:  <%-{-{-{-<

      That is very useful, thank you!