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..$#+; }