Indeed. That was the main point I was going to contribute, but you've beat me to it.
The OP even specifically says he wants to match "any number of characters not containing Z", which is properly written [^Z]*. .*?Z means "the shortest available sequence of any character at all (except, usually, newlines), followed by a Z", which is not quite the same thing. | [reply] [d/l] [select] |
>perl -wMstrict -le
"'AZ' =~ /A( [^Z]*)?Z/;
print qq{'$1'};
"
Use of uninitialized value $1 in concatenation (.) or string at ...
''
'Elegance', admittedly protean, would yet seem to require avoiding this extra step.
| [reply] [d/l] [select] |
| [reply] [d/l] |
Huh.
I prefer a correct solution that contains some repetition to an incorrect solution any time. There are ways to make .*? work correctly, but they include a certain amount of backtracking control, which makes them harder to maintain.
This is true especially if Z happens not to be just a latter, but a more complicated pattern.
If that's the case, you should use interpolation anyway, and [^Z]+ is to be replaced by (?s:(?!$Z).)+
In general it does make a difference if Z is actually a single character or something else, and if it's something else that should be mentioned in the original question anyway.
Update: added missing quantifier
Perl 6 - links to (nearly) everything that is Perl 6.
| [reply] [d/l] [select] |
| [reply] [d/l] [select] |
This is true especially if Z happens not to be just a latter, [sic] but a more complicated pattern.
To expand on moritz's Re^5: This regexp made simpler: This, I think, is exactly the motivation behind regex objects. Using really non greedy match as an example, some regex objects can be factored out and treated as if they were atomic – because they are, more or less! (The big gotcha is that things get tricky if the factored regexes contain capturing groups, which consequently should be avoided. This problem is ameliorated by 5.10's relativistic approach to referencing capture variables.) The 'regex factoring' approach can lead to a lot more initial verbosity, but this cost is repaid many-fold by greater ease in conceptualizing, building and maintaining complex regexes.
In the example below, $not_S* and $not_S*? work as one would expect for .* and .*? expressions. (There is a problem with the counting quantifiers {n} et al: something like $not_S{3} looks like a hash element; the more awkward (?:$not_S){3} must be used instead.)[See update] Note that something like $S or $E could be a much more complicated (and factored) pattern.
>perl -wMstrict -le
"$_ =
'no a START no b START yes c END maybe d END no e START yes f END';
my $S = qr{ START }xms;
my $E = qr{ END }xms;
my $not_S = qr{ (?! $S) . }xms;
my $Lazy = qr{ $S $not_S*? $E }xms;
print qq{'$_'};
print 'greedy: ', map qq{'$_' }, m{ $S $not_S* $E }xmsg;
print 'lazy: ', map qq{'$_' }, m{ $S $not_S*? $E }xmsg;
print 'compound: ', map qq{'$_' }, m{ $Lazy }xmsg;
"
'no a START no b START yes c END maybe d END no e START yes f END'
greedy: 'START yes c END maybe d END' 'START yes f END'
lazy: 'START yes c END' 'START yes f END'
compound: 'START yes c END' 'START yes f END'
Update: Somehow I had the idea that $scalar{3} in a regex would interpolate like a hash element, but I just tested this in 5.10 and AS 5.8.9 and 'taint so. Where did I get this notion? Update: Ah, $scalar{'7'} and $scalar{$n} interpolate like hash elements and (?:$scalar){$n} looks like a quantifier again; problem solved.
| [reply] [d/l] [select] |