Here's one way to do it:
#!/usr/bin/perl
while (<STDIN>) {
chomp;
if (/(\b\w*a\b)/) {
print "Matched: |$`<$&>$'|\n";
print "\$1 contains '$1'\n"; # The new output line
} else {
print "No match: |$_|\n";
}
}
This is the same test program (with a new pattern), except that the one marked line has been added to print out $1.
The pattern uses a pair of \b word-boundary anchors inside the parentheses though the pattern works the same way when they are placed outside. That's because anchors correspond to a place in the string but not to any characters in the string: anchors have "zero width."