in reply to Looking for alternative syntax for assigning variables based on regex matches
With non-destructive /r modifier (and lookahead):
*Updated
+one more way:
OUTPUT:perl -wle '$foo = "a hello w"; $foo = $foo =~ s/(?=.*(hello)).*/$1/sr; + print $foo'
And if you want only to use your capture, just:hello
In the later example a value of $foo didn't change....; print $foo =~ s/(?=.*(hello)).*/$1/sr;
*Updated
+one more way:
Though it looks like bad practice to change a variable while it is in use....; $foo =~ /hello(?{ $foo = $& })/; print $foo
|
---|
In Section
Seekers of Perl Wisdom