Hi

I'm failing to use relative backreferences in \g-form in character classes.

Please note how [^\1] still works, but [^\g-1] fails.

Is there a better syntax within character classes?

I'm afraid I need to switch to negated look-aheads or hardcode different quotes.°

Debugger demo with perl-de0

The following regex tries to match quoted substrings in $1, the quote in $2

DB<164> p $_ __'abc'__"def"__ DB<165> x / ( (['"]) [^\2]*? \2 ) /gx # OK 0 '\'abc\'' 1 '\'' 2 '"def"' 3 '"' DB<166> x / ( (['"]) [^\2]*? \g-1 ) /gx # OK 0 '\'abc\'' 1 '\'' 2 '"def"' 3 '"' DB<167> x / ( (['"]) [^\g-1]*? \g-1 ) /gx # OOPS Invalid [] range "\g-1" in regex; marked by <-- HERE in m/ ( (['"]) [^ +\g-1 <-- HERE ]*? \g-1 ) / at (eval 179)[c:/St\ rawberry/perl/lib/perl5db.pl:738] line 2. at (eval 179)[c:/Strawberry/perl/lib/perl5db.pl:738] line 2. eval 'no strict; ($@, $!, $^E, $,, $/, $\\, $^W) = @DB::saved; +package main; $^D = $^D | $DB::db_stop; / ( ([\'"]) [^\\g-1]*? \\g-1 ) /gx; ' called at c:/Strawberry/perl/lib/perl5db.pl line 738 DB::eval called at c:/Strawberry/perl/lib/perl5db.pl line 3138 DB::DB called at -e line 1 DB<168> p $] 5.032001 DB<169>

FWIW: \g{-1} instead of \g-1 doesn't help either

This question is a follow-up to solution wanted for break-on-spaces (w/specifics)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

update

°) or using (??{..})

SOLUTION

escaping the dash inside character classes helps, otherwise it's seen as a range

DB<170> x / ( (['"]) [^\g\-1]*? \g-1 ) /gx 0 '\'abc\'' 1 '\'' 2 '"def"' 3 '"' DB<171> x / ( (['"]) [^\g{\-1}]*? \g-1 ) /gx 0 '\'abc\'' 1 '\'' 2 '"def"' 3 '"' DB<172> x / ( (['"]) [^\g2]*? \g-1 ) /gx 0 '\'abc\'' 1 '\'' 2 '"def"' 3 '"' DB<173>

In reply to (SOLVED) RegEx: backreferences in character classes by LanX

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.