If I have a match operator, how do I save the parts of the strings captured in the parentheses in variables instead of using $1
, $2
, and so on?
Usually you also want to do a test to make sure the input string matches your regular expression. That way you can also handle error cases.
To extract something interesting you also need to have some way to anchor the bit you're interested in extracting.
So, with your example, this will first make sure the input string matches our expression, and then extract the bit between the two 'boring' bits:
$input = "boring interesting boring";
if($input =~ m/boring (.*) boring/) {
print "The interesting bit is $1\n";
}
else {
print "Input not correctly formatted\n";
}