Another take on it:

Unfortunately regexes don't support backreferences in character classes - like [^\g1] - to forbid the delimiter inside the string. (at least I couldn't find it.)

But it's possible to have the same effect with negative lookaheads

DB<34> $_='abbbbbbbbbba' DB<35> x m/^(.) ( (?!\1) . )* \1$/x 0 'a' 1 'b'

NB a lookahead doesn't move the position, that's why it has to be moved with an .

And this approach seems to work in your code:

#!/usr/bin/perl use strict; use warnings; my @cases = ( q{'abc"def'}, q{'abc'}, q{"abc"}, q{''}, q{'abc'def'}, # Want this to fail matching q{'This shouldn't match'}, # Want this to fail matching q{"This isn't a problem"}, q{"abc}, q{abc"}, q{abc}, q{'abc"}, q{'ab''}, # Want this to fail matching ); strip_quotes($_) for @cases; # If we can remove a matching pair of single or double quotes from # a string, without the quote symbol also appearing within the string, # do so. Otherwise don't change the string. sub strip_quotes { my $line = shift; print "\n$line\n"; # NO NEGATIVE LOOKAHEAD # This works except it allows an embedded delimiter if ( $line =~ m{^ # anchor ( # capture delimiter in pos 1 ["'] # delim is single or double quote ) (.*) # anything \g1$}x # finally, the delim ) { print " 1- Got a match: delimiter was {$1}, body was {$2}\n"; } else { print " 1- No match.\n"; } # ATTEMPTING NEGATIVE LOOKAHEAD # This should fail if the delimiter is found in non-terminal pos. if ( $line =~ m{^ # anchor start ( # capture delimiter in pos 1 ["'] # delim is single or double quote ) ( (?: # --- negate backrefrence (?!\g1) # following letter is not delim . # consume following letter )* ) \g1 # finally, the delim $ # anchor end }x ) { print " 2- Got a match: delimiter was {$1}, body was {$2}\n"; } else { print " 2- No match.\n"; } }

'abc"def' 1- Got a match: delimiter was {'}, body was {abc"def} 2- Got a match: delimiter was {'}, body was {abc"def} 'abc' 1- Got a match: delimiter was {'}, body was {abc} 2- Got a match: delimiter was {'}, body was {abc} "abc" 1- Got a match: delimiter was {"}, body was {abc} 2- Got a match: delimiter was {"}, body was {abc} '' 1- Got a match: delimiter was {'}, body was {} 2- Got a match: delimiter was {'}, body was {} 'abc'def' 1- Got a match: delimiter was {'}, body was {abc'def} 2- No match. 'This shouldn't match' 1- Got a match: delimiter was {'}, body was {This shouldn't match} 2- No match. "This isn't a problem" 1- Got a match: delimiter was {"}, body was {This isn't a problem} 2- Got a match: delimiter was {"}, body was {This isn't a problem} "abc 1- No match. 2- No match. abc" 1- No match. 2- No match. abc 1- No match. 2- No match. 'abc" 1- No match. 2- No match. 'ab'' 1- Got a match: delimiter was {'}, body was {ab'} 2- No match.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!


In reply to Re: Using negative lookahead by LanX
in thread Using negative lookahead by ibm1620

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.