You need to escape the special characters in the regex:
(coor (1) (2)) => \(coor (1) (2)\)
same for /[/ that is a syntax error => /[/
Here is my modified version of your script, the regex is fixed, I split the string on ',' to match the intended keyword and the regex result is tested:
#!/usr/bin/perl
my $keyword = "object1";
my $str = "[(beginKey object1 (coor 141 257) (coor 1315 254) (coor 1313 430) (coor 140 420) [] [] []), (beginKey keyword (coor 2035 253) (coor 1315 254) (coor 1313 430) (coor 2034 436) [] [] [])].";
my @entries=split(',', $str);
foreach my $entry (@entries)
{
if ($entry =~ /$keyword/)
{
my $tomatch=$';
if ($tomatch =~ /\(coor\s(\d+)\s(\d+)\)\s\(coor\s(\d+)\s(\d+)\)\s\(coor\s(\d+)\s(\d+)\)\s\(coor\s(\d+)\s(\d+)\)\s/)
{
print $1," ";
print $2," ";
print $3," ";
print $4," ";
print $5," ";
print $6," ";
print $7," ";
print $8," ";
}
else
{
print "no match !";
}
}
}
This prints:
141 257 1315 254 1313 430 140 420