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

$var =~ /(["'])[^(??{"$1"})]*?\1/; which worked unless $1 contained a special character (forgot the character, but it wasn't a quote),

I don't quite understand this? How could $1 contain anything other than ["']?

I'm just a beginner

As am I.

This is my attempt at capturing simple quoted elements - Ie. those without embedded quotes

# Doesn't handle embedded like-quotes my $re_simpleQuoted = qr[ ( ["'] ) ( (??{ "[^\1]+?" }) ) \1 ]x;

In order to handle embedded quotes you need to go a stage further. The code below seems to handle most cases I have tried of either or both embedded quotes like this "...""..." or '...''....''....', which IMO is the rational way to do it outside of perl source code, and also done this way, "...\"..." and '...\'...'.

It doesn't handle basket cases where an escaped quote is immediately preceded by an escaped escape. Eg. "...\\\"...\"...". This could be addressed, but I think sufficiently rare that I wouldn't bother unless I had specific knowledge that it would arise. There are probably others that it would fail on too.

Code

#! perl -slw use strict; require 5.008; my $re_quoted = qr[ (["']) #'" (?{ local $q = $^N }) ( (??{ "(?x: [^$q] | (?: (?<!$q)(?: (?: $q ){2} )+ ) | (?: \\$q ) )+" }) ) (?{ $quoted = $^N }) (??{ "$q" }) ]x; while( <DATA> ){ chomp; our $quoted; print; print "\t<$quoted>" while m[$re_quoted]g; print ''; } __DATA__ unquoted stuff "quoted stuff" unquoted stuff unquoted stuff 'quoted stuff' unquoted stuff unquoted stuff "quoted stuff with embedded 'alternate' quotes" unquote +d stuff unquoted stuff 'quoted stuff with embedded "alternate" quotes' unquote +d stuff unquoted stuff "quoted stuff with embedded ""like"" quotes" unquoted s +tuff unquoted stuff 'quoted stuff with embedded ''like'' quotes' unquoted s +tuff unquoted stuff "quoted stuff with embedded """"like"""" quotes" unquot +ed stuff unquoted stuff 'quoted stuff with embedded ''''like'''' quotes' unquot +ed stuff unquoted stuff "quoted 'stuff' with escaped \"like\" quotes" unquoted +stuff unquoted stuff 'quoted 'stuff' with escaped \'like\' quotes' unquoted +stuff unquoted stuff "quoted stuff with embedded ""like"" quotes and escaped + \"like\" quotes" unquoted stuff unquoted stuff 'quoted stuff with embedded ''like'' quotes and escaped + \'like\' quotes' unquoted stuff

Output

D:\Perl\test>quoted.pl8 unquoted stuff "quoted stuff" unquoted stuff <quoted stuff> unquoted stuff 'quoted stuff' unquoted stuff <quoted stuff> unquoted stuff "quoted stuff with embedded 'alternate' quotes" unquote +d stuff <quoted stuff with embedded 'alternate' quotes> unquoted stuff 'quoted stuff with embedded "alternate" quotes' unquote +d stuff <quoted stuff with embedded "alternate" quotes> unquoted stuff "quoted stuff with embedded ""like"" quotes" unquoted s +tuff <quoted stuff with embedded ""like"" quotes> unquoted stuff 'quoted stuff with embedded ''like'' quotes' unquoted s +tuff <quoted stuff with embedded ''like'' quotes> unquoted stuff "quoted stuff with embedded """"like"""" quotes" unquot +ed stuff <quoted stuff with embedded """"like"""" quotes> unquoted stuff 'quoted stuff with embedded ''''like'''' quotes' unquot +ed stuff <quoted stuff with embedded ''''like'''' quotes> unquoted stuff "quoted 'stuff' with escaped \"like\" quotes" unquoted +stuff <quoted 'stuff' with escaped \"like\" quotes> unquoted stuff 'quoted 'stuff' with escaped \'like\' quotes' unquoted +stuff <quoted 'stuff' with escaped \'like\' quotes> unquoted stuff "quoted stuff with embedded ""like"" quotes and escaped + \"like\" quotes" unquoted stuff <quoted stuff with embedded ""like"" quotes and escaped \"like +\" quotes> unquoted stuff 'quoted stuff with embedded ''like'' quotes and escaped + \'like\' quotes' unquoted stuff <quoted stuff with embedded ''like'' quotes and escaped \'like +\' quotes>

The PITA of using capturing parens to establish which quote to look for is that it limits the use when combining regexes together as you never know which $n var you captured into.

The 5.8 addition of the $^N variable has made this easier and the use of the experimental features more useful.

The logical extension (IMO) to this would be to extend the non-capturing group syntax to allow capturing to a user defined variable (our vars make sense I think).

Something like

(?: ... :$captured)

The other extension that I would like to see is a way of specifying a variable length match-anything-except-this construct. Like [^"] but longer

(?>>stop when you encounter this)*

Essentially, a non-zero length positive look ahead assertion! :)

Makes sense to me, but then I have no idea of the costs/difficulties these would impose on the regex engine.

T'would be nice though.


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

Replies are listed 'Best First'.
Re^5: Perlre interpretation required (perfect dequote regex)
by Aristotle (Chancellor) on May 04, 2003 at 04:35 UTC
    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.

      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.

Re: Re: Re: Re: Re: Perlre interpretation required
by Wassercrats (Initiate) on May 04, 2003 at 06:03 UTC
    $var =~ /(["'])[^(??{"$1"})]*?\1/; which worked unless $1 contained a +special character (forgot the character, but it wasn't a quote),

    I don't quite understand this? How could $1 contain anything other than ["']?


    I guess $1 had to be a quote, but in the error message, $1 was replaced with the string of matched characters, which were all non-quotes since I negated $1 in the regex. I don't recall the entire [^(??{"$1"})] being replaced, which makes it seem like the string of non-quotes in the error message was made of characters from only $1.

    Anyway, it appears as though the characters matching ^$1 in my regex were interpolated, causing errors, and quoting them in the regex would have quoted the square brackets, the ^, the (??{", etc. and prevented the very character that I really wanted to quote from being matched.

    My simple .*? between the quotes works for me, and my other idea is overkill for my current project anyway. I'll analyze the solutions from this thread when I have a chance. I'm sure they solve the problem.

      Understood. The errors aren't always as clear as you would hope, though perl seems to do a better job in most cases than many other languages I've encountered.


      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