Use a backreference.
if (!$flag && /($octet)/){
print "$1\n";
}
From Camel 3, p. 41:
A pair of parenthesis around a part of a regular expression causes whatever was matched by that part to be remembered for later use. $1 is the match from the first set of parenthesis in your regexp,
$2 the second, and so on.