in reply to Re: Re: Re: Re: Perlre interpretation required
in thread Perlre interpretation required

With a bit of thought, it is quite possible to write an unbreakable regex de-quoter:
#!/usr/bin/perl -wnl use strict; print; print join ' / ', map qq["$_"], m[\G ( ' (?: \\ . | '' | [^'] ) + ' | " (?: \\ . | "" | [^"] ) + " | (?: \\ . | [^'"] ) + ) ]gx; print "";
It will handle even the edge cases yours breaks on. Using the (?{}) stuff to remove the redundancy is a nice idea, but for this case isn't the best solution.
sub make_dequote_rx { my @char = map quotemeta, @_; my $chars = join '', @char; my $rx = join( ' | ', map(qq[$_ (?: \\\\ . | $_$_ | [^$_] ) + $_], @char), qq[(?: \\\\ . | [^$chars] ) +], ); return qr/\G ( $rx )/x; } my $dequote = make_dequote_rx qw(' ");
Update: Doubled the backslashes in the second piece of code.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re^5: Perlre interpretation required
by BrowserUk (Patriarch) on May 04, 2003 at 09:43 UTC

    Sorry Aristotle, but this is far from "unbreakable". In fact it doesn't even compile? You can't apply the /g modifier to qr//.

    Your version

    Output

      Have you tried the first snippet? It is unbreakable. I didn't test the second though, sigh. I forgot that I'm using the doublequote-like op to put together a regex, so I need to double the backslash both for the regex as well as for the doublequotes, meaning I need four backslashes to begin with in order to get one literal one in the end. Let me fix that..

      Makeshifts last the longest.

        I guess I must be using it wrong, or the two routines aren't meant to acheive the same thing. As-is, yours does find and match on the quoted sections and handled the embedded quotes ok -- although it does leave the leading and trailing quotes in place -- but it also matches on the unquoted sections?

        I get the impression that we are both acheiving our goals adequately, they are just different goals:)

        Testcode

        Results


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller