oysterperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a file with the lines below:
Grab this 'test1' (repeat/new) many lines ... .. .. Grab this 'test2' (repeat/new) many lines ... .. .. Grab this 'test3' (repeat/new)
I want to get all test* in a variable. I am trying the code below:
while ($current_line = <$file_pointer>) { if ($current_line =~ /Grab this\s+(.*)/) { $GRAB = $1; push (@grab_array, $GRAB); print "$GRAB"; } }
With this, grab_array shows the entire line in it like: 'test3' (repeat/new) I don't want the (repeat/new) but only the test* in grab_array. I have tried the following: $current_line =~ /Grab this\s+(.+)/ $current_line =~ /Grab this\s+(.)/ but it did not work. Appreciate any help with this. Thanks.

Replies are listed 'Best First'.
Re: How to get only the next word matching the regex in the line
by GrandFather (Saint) on Nov 11, 2019 at 02:39 UTC

    .* 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

      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!
Re: How to get only the next word matching the regex in the line
by LanX (Saint) on Nov 11, 2019 at 02:28 UTC
    Please try a non-greedy match

      /Grab this\s+'(.*?)'/

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      That worked, thanks!
        Great!

        Do you understand why? :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: How to get only the next word matching the regex in the line
by swl (Prior) on Nov 11, 2019 at 02:27 UTC

    If it is consistently formatted then you can be more specific with your capture and look for 'test\d+'.

    $current_line =~ /Grab this\s+('test\d+')/