Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by haukex (Bishop)
on Mar 02, 2021 at 05:24 UTC ( #11128988=note: print w/replies, xml ) Need Help??


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

my ($x,$y) = $foo =~ /(hello)(world)/ or die "failed to match"; if ( my ($x,$y) = $foo =~ /(hello)(world)/ ) { print $x, $y; } if ( $foo =~ /(?<x>hello)(?<y>world)/ ) { print $+{x}, $+{y}; }

Update: The first two work like any other variable assignment, so ($foo) = $foo=~/(hello)/; works too of course. (Update 2: If assigning back to the same one variable, so does s/// and its variations.)

Replies are listed 'Best First'.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-04-13 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?