Open p5pRT opened 22 years ago
$ perl -wle '/(?{1})?/'
results in
Quantifier unexpected on zero-length expression in regex; marked by \<-- HERE in m/(?{1})? \<-- HERE / at -e line 1.
While this is a useful (fatal) error for expressions without side-effects\, it's a nuisance for expressions with side-effects. And doing a (?{ }) without side-effects is kind of useless...
The Perl regex machine is a nice framework to do backtracking with. And then it would be useful to be able to do:
(?{ CODE })?
There is a workaround of course:
(?:(?{ CODE })|)
but that's more verbose.
On Thu\, Jul 11\, 2002 at 09:50:28AM -0700\, Abigail wrote:
$ perl \-wle '/\(?\{1\}\)?/'
results in
Quantifier unexpected on zero\-length expression in regex; marked by \<\-\- HERE in m/\(?\{1\}\)? \<\-\- HERE / at \-e line 1\.
While this is a useful (fatal) error for expressions without side-effects\, it's a nuisance for expressions with side-effects. And doing a (?{ }) without side-effects is kind of useless...
The Perl regex machine is a nice framework to do backtracking with. And then it would be useful to be able to do:
\(?\{ CODE \}\)?
There is a workaround of course:
\(?​:\(?\{ CODE \}\)|\)
But (?{ }) always succeeds. The only way for (?{ }) to be backtracked is for the surrounding regex to backtrack.
Ronald
Abigail \abigail@​foad\.org wrote: : $ perl -wle '/(?{1})?/' : :results in : : Quantifier unexpected on zero-length expression in regex; : marked by \<-- HERE in m/(?{1})? \<-- HERE / at -e line 1. : : :While this is a useful (fatal) error for expressions without side-effects\, :it's a nuisance for expressions with side-effects. And doing a (?{ }) :without side-effects is kind of useless...
Presumably the only relevant side-effects are the ones that occur under backtracking\, ie rollback of local()ised variables. If I understand you correctly you want to be able to say: local $count = 0; "foobar" =~ m{ (?{ local $count = 1 })? foo (??{ $count ? 'baz' : 'bar' }) }; ... and have it match 'foobar' the second time through. Is that right?
Do you have a more concrete example that cannot easily be expressed using the currently supported syntax?
What would you want m/(?{1})*/ to do? The same as C\< 1 while 1 >?
:Flags: : category=core : severity=medium
As far as I am aware we consciously chose to impose this restriction\, so I'd class this as a wishlist item rather than a bug. If the restriction is underdocumented (which is quite likely)\, then the docs need fixing.
Hugo
On Thu\, Jul 11\, 2002 at 01:10:27PM -0400\, Ronald J Kimball wrote:
On Thu\, Jul 11\, 2002 at 09:50:28AM -0700\, Abigail wrote:
$ perl \-wle '/\(?\{1\}\)?/'
results in
Quantifier unexpected on zero\-length expression in regex; marked by \<\-\- HERE in m/\(?\{1\}\)? \<\-\- HERE / at \-e line 1\.
While this is a useful (fatal) error for expressions without side-effects\, it's a nuisance for expressions with side-effects. And doing a (?{ }) without side-effects is kind of useless...
The Perl regex machine is a nice framework to do backtracking with. And then it would be useful to be able to do:
\(?\{ CODE \}\)?
There is a workaround of course:
\(?​:\(?\{ CODE \}\)|\)
But (?{ }) always succeeds. The only way for (?{ }) to be backtracked is for the surrounding regex to backtrack.
Well\, yes. Consider the following code to generate all combinations of a 4 element set:
"" =~ /(?{ @x = qw !A B C D!; @y = (0) x 4; }) (?:(?{ local $y [0] = 1 })|) (?:(?{ local $y [1] = 1 })|) (?:(?{ local $y [2] = 1 })|) (?:(?{ local $y [3] = 1 })|) (?(?{ print "@x[grep {$y [$_]} 0 .. 3]\n" }) fail )/x;
I'd find it more logical to be able to write it as:
"" =~ /(?{ @x = qw !A B C D!; @y = (0) x 4; }) (?{ local $y [0] = 1 })? (?{ local $y [1] = 1 })? (?{ local $y [2] = 1 })? (?{ local $y [3] = 1 })? (?(?{ print "@x[grep {$y [$_]} 0 .. 3]\n" }) fail )/x;
as '?' indicates "first try matching\, then try without".
Abigail
On Thu\, Jul 11\, 2002 at 06:28:02PM +0100\, Hugo van der Sanden wrote:
Abigail \abigail@​foad\.org wrote: : $ perl -wle '/(?{1})?/' : :results in : : Quantifier unexpected on zero-length expression in regex; : marked by \<-- HERE in m/(?{1})? \<-- HERE / at -e line 1. : : :While this is a useful (fatal) error for expressions without side-effects\, :it's a nuisance for expressions with side-effects. And doing a (?{ }) :without side-effects is kind of useless...
Presumably the only relevant side-effects are the ones that occur under backtracking\, ie rollback of local()ised variables. If I understand you correctly you want to be able to say: local $count = 0; "foobar" =~ m{ (?{ local $count = 1 })? foo (??{ $count ? 'baz' : 'bar' }) }; .. and have it match 'foobar' the second time through. Is that right?
Yes.
Do you have a more concrete example that cannot easily be expressed using the currently supported syntax?
Well\, as I said\, there's a workaround\, so it's always expressable in the currently supported syntax. But expressing (?{ CODE }){100\,200} as a sequence of | clauses is a bit awkward.
What would you want m/(?{1})*/ to do? The same as C\< 1 while 1 >?
Yes.
Abigail
Abigail \abigail@​foad\.org wrote: :On Thu\, Jul 11\, 2002 at 06:28:02PM +0100\, Hugo van der Sanden wrote: :> Do you have a more concrete example that cannot easily be expressed :> using the currently supported syntax? : :Well\, as I said\, there's a workaround\, so it's always expressable in :the currently supported syntax. But expressing (?{ CODE }){100\,200} :as a sequence of | clauses is a bit awkward.
Hence 'cannot _easily_ be expressed'.
:> What would you want m/(?{1})*/ to do? The same as C\< 1 while 1 >? : :Yes.
I think this is in large part what the current behaviour aims to guard against.
I could imagine supporting this in 5.10 by way of a new switch\, but I do not think it would be wise to change the default behaviour. I would also be tempted to allow any such switch also to turn on the currently disabled backtracking through assertions\, which would allow patterns such as /(?=.*(.))/ to backtrack and find alternate ways of matching the assertion.
Hugo
removing this ticket from the (?{}) metabug\, as its a wishlist rather than a bug
Migrated from rt.perl.org#10040 (status was 'open')
Searchable as RT10040$