Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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 ( [id://11108532]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11108532]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found