my $pat = qr/\w+ ((w)hite|(b)lack)/;
$_ = 'mostly black';
print "$1 starts with $+\n" if /$pat/;
$_ = 'but some white spaces';
print "$1 starts with $+\n" if /$pat/;
####
my $pat = qr/\w+ ((w)hit(e)|(b)lac(k))/;
$_ = 'mostly black';
my($word, $first, $last) = grep defined $_, /$pat/;
print "$word starts with $first and ends with $last\n" if $word;
$_ = 'but some white spaces';
($word, $first, $last) = grep defined $_, /$pat/;
print "$word starts with $first and ends with $last\n" if $word;
####
my $pat = qr/\w+ ((w)hit(e)|(b)lac(k))/;
$str = 'mostly black but some white spaces';
while ($str =~ /$pat/g) {
my @m = get_captures($str);
print "$m[1] starts with $m[2] and ends with $m[3]\n";
}
sub get_captures {
map { defined $+[$_]
? substr($_[0],$-[$_],$+[$_]-$-[$_])
:()
} 0..$#+;
}