in reply to Interesting behavior of regular expression engine
I'm no expert, but use re 'debug' seems to offer some insight.
In both cases the last part of the regexp is the longest floating string, so is the part that Perl attempts to match first.
In the first case, all that remains is to match "a" against /.+/. This succeeds, and your code returns true (say returns true if it is able to print anything), thus the whole match succeeds. The code only needs to be executed once.
In the second case, it tries to slot letters into two patterns: /.+/ before the code and /./ after the code, and it has to shuffle them around six times before it gets a success.
Personally, I think it's a bad idea to rely on code embedded in regexps having side-effects. How many times it will be executed, and in what order is pretty unpredictable; a new Perl release could change it.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Interesting behavior of regular expression engine
by rjt (Curate) on Mar 12, 2013 at 23:51 UTC | |
by 7stud (Deacon) on Mar 14, 2013 at 07:48 UTC | |
by Anonymous Monk on Mar 14, 2013 at 08:00 UTC | |
by Anonymous Monk on Mar 14, 2013 at 08:13 UTC | |
Re^2: Interesting behavior of regular expression engine (last not matched first)
by tye (Sage) on Mar 13, 2013 at 05:52 UTC |