Below perl script is printing the whole string as an output,But I was expecting output as Perl.I am new to perl as well as regex.Can someone please explain me why I am not getting the expected output.
my $txt='I am learning Perl';
$txt=~/(\w+)$/;
print $txt;
OutPut;
I am learning Perl
Output I was expecting:
Perl
As per my knowledge the output should be a word containing one or more alphanumeric characters and the search for the pattern begins from the end.I don’t understand where I am wrong here .
A regular expression match doesn’t modify the variable. It returns either a list of matches or a boolean depending on the context.
Use it in list context to capture the matches.