The perlop says:
If the pattern evaluates to the empty string, the last successfully executed regular expression is used instead.
This seems to be true in a simple example:
The first match is successful, and all the a's that match the same regex are printed.$ perl -wle '"a" =~ /a/; // and print for qw( a b a b a b )' a a a
It behaves correctly when combined with the flip flop operator, too, making it possible to treat the boundary lines specially:
$ perl -le 'print for a .. z' | perl -nle 'if (/d/ .. /h/) { print un +less // }' e f g
It doesn't work as expected when combined with next, though:
$ perl -le 'print for a .. z' | perl -nle 'if (/d/ .. /h/) { next unl +ess //; print }' d f g h
Why is "f" printed?
When combined with -Mre=debug, it shows
... Matching REx "d" against "d" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave 0 Found anchored substr "d" at offset 0 (rx_origin now 0)... (multiline anchor test skipped) Intuit: Successfully guessed: match at offset 0 Matching REx "h" against "d" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave -1 Did not find anchored substr "h"... Match rejected by optimizer Matching REx "d" against "d" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave 0 Found anchored substr "d" at offset 0 (rx_origin now 0)... (multiline anchor test skipped) Intuit: Successfully guessed: match at offset 0 Matching REx "h" against "e" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave -1 Did not find anchored substr "h"... Match rejected by optimizer Matching REx "d" against "e" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave -1 Did not find anchored substr "d"... Match rejected by optimizer Matching REx "h" against "f" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave -1 Did not find anchored substr "h"... Match rejected by optimizer Matching REx "" against "f" (*) 0 <> <f> | 0| 1:NOTHING(2) 0 <> <f> | 0| 2:END(0) Match successful! ...
Why is there the empty regex (see (*))? Is the magic of // somehow broken by next? Is this the expected behaviour and is it documented anywhere?
Update: When combined with continue, the output changes.
Note that the continue part is empty!$ perl -le 'print for a .. z' | perl -ne 'if (/d/ .. /h/) { next unle +ss //; print }} continue {' d e f g h
In reply to Empty pattern in regex by choroba
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |