On Wed, Jun 30, 2021 at 03:05:04PM +0200, Eirik Berg Hanssen wrote: > On Wed, Jun 30, 2021 at 12:42 PM Nicholas Clark <nick@ccl4.org> wrote: > > > It seems that if $ is matched, it needs to be the (logical) end of the > > pattern, else the entire pattern matches. By "logical", I mean that other > > text follow in the regex, but it needs to be other alternations, or > > something > > else that doesn't contribute to matching. It can't be more characters, even > > if they are present on the next line. > > > > You can match more characters, but you need to consume the newline > first. (The $ is a zero-width lookahead assertion – it does not consume > anything.) D'oh! Thanks. Yes. That was what I goofed. So, based on your suggestion I've written what I think is a fix, and pushed it to https://github.com/nwc10/perl5/tree/DD-84569 The pure-Perl implementation is this: $pat =~ s < (\\.) # anything backslash escaped | (\$)(?![)|]|\z) # any unescaped $, except $| $) and end | / # any unescaped / > { $1 ? $1 : $2 ? '${\q($)}' : '\\/' }gex; The C implementation might actually be easier to read. :-) It passes tests on blead. I've not *yet* tested it against older Perls, so I'm not going to roll a CPAN dev release yet. (Also, a fresh head tomorrow might spot some bugs or corner cases that I missed) Thanks for your insights on this. Nicholas ClarkThread Previous | Thread Next