Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Looking for alternative syntax for assigning variables based on regex matches

by rsFalse (Chaplain)
on Mar 02, 2021 at 10:28 UTC ( [id://11129009]=note: print w/replies, xml ) Need Help??


in reply to Looking for alternative syntax for assigning variables based on regex matches

With non-destructive /r modifier (and lookahead):
perl -wle '$foo = "a hello w"; $foo = $foo =~ s/(?=.*(hello)).*/$1/sr; + print $foo'
OUTPUT:
hello
And if you want only to use your capture, just:
...; print $foo =~ s/(?=.*(hello)).*/$1/sr;
In the later example a value of $foo didn't change.
*Updated

+one more way:
...; $foo =~ /hello(?{ $foo = $& })/; print $foo
Though it looks like bad practice to change a variable while it is in use.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-18 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found