The problem actually has nothing to do with greed / laziness (which applies only to repetition operators like * or + ).
The problem is two empty fields next to each other: |-|-| . The first one is mapped, but then the second one fails because opening | already in use, but since you have a beining-of-line token in the rule ^-| he does not fit this.
I think a much simpler approach would be to split your input into | , and then search for any fields consisting only of - :
my $count = 0; foreach (split(/\|/,$string)) { if( /^-$/ ) { $count++; } }
There really is no way to effectively implement this with a regex, since Perl does not support variable-length hangs (at least as far as I know). One way to βtrickβ would be to add | at the beginning and end, then you could successfully use the lookbehind / lookahead statements:
$string = "|$string|"; my $count = () = $string=~/(?<=\|)-(?=\|)/g;
(The following is an alternative solution that uses alternative statements without changing without changing the line, so I made a mistake when I said βthere is no way to implement this with a regular expression.β I think splitting into | is the best way to solve this problem.)
source share