my $str = 'xyzxyzxyz - Prod Cache Broker - 16100'; print "$1\n" if ($str =~ m/^(\S+)\s+?.+$/);
The problem you were having is that (.+) is greedy, so it is consuming "xyzxyzxyz - Prod Cache Broker - 16100", and then the \s+? was forcing it to give up the space immediately preceding "16100", and finally the .+ was grabbing "16100".
If you wanted the parens to capture only up to (but not including) the first space, just tell it to match non-space characters.
Dave
In reply to Re: minimal matching regex
by davido
in thread minimal matching regex
by fionbarr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |