in reply to Re^2: A (non) reg-ex question
in thread A (non) reg-ex question
I think my edition of the Camel predates qr{} so I doubt I have that example to hand, but I'd expect it to be much the same except that balanced parens can usually look like "()(())(()())" and the like.
Except for certain special optimisations, a regular expression is always traversed from left to right. The critical thing to stop infinite recursion is to ensure that at least one character must be matched each time before recursing, ie that there be something to the left of the nested $re.
In this case, ($re = qr{(?:0(??{$re})1)?}), we require a '0' to be matched before recursing. So "0011" =~ /^$re\z/ will be executed like:
anchor to start try $re match '0' try $re match '0' try $re fail to find '0' group is optional, matched zero times $re matched '' match '1' group is optional, matched one time $re matched '01' match '1' group is optional, matched one time $re matched '0011' anchor to end match complete: matched '0011' from start
Similarly, you could (foolishly) replace /0+/ with a recursive regexp like:
which would work, but the following would fall into infinite recursion:$re = qr{0(??{$re})?};
$re = qr{(??{$re})?0};
Hope this helps,
Hugo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: A (non) reg-ex question
by johngg (Canon) on Mar 21, 2006 at 00:11 UTC |