in reply to Re^2: How we can separate a backref from a digit?
in thread How we can separate a backref from a digit?

Thanks, but \1+ is not correct.

Sorry that one of the 5 methods didn't meet with your approval.

By way of compensation, here's five more that might not:

print "Match1\n" if "aa1" =~ /^(a)\1[1]$/;; Match1 print "Match1\n" if "aa1" =~ /^(a){2}1$/;; Match1 print "Match1\n" if "aa1" =~ /^(a)\1(?=)1$/;; Match1 print "Match1\n" if "aa1" =~ /^(a)\1(?<=)1$/;; Match1 print "Match1\n" if "aa1" =~ /^(a)\1(?{})1$/;; Match1

I think I prefer the first of these.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: How we can separate a backref from a digit?
by raybies (Chaplain) on Mar 18, 2011 at 12:48 UTC
    C'mon... now you're just showing off... :-D (Btw, this is a great thread... thanks for asking the questions, guys... I copied this one into my personal folder...) --Ray

    Update: here're my couple to add...

    print "Match1" if "aa1" =~ /^(a)\1(1)$/; #of course this places 1 in $ +2
    another silly one...
    print "match" if "aa1" =~ /^(a)\1(?:[^\s\S]*)1$/; #buwahahaha... kind +of the opposite of Perl Golf...

      FWIW: I now think that the "proper" way to do this is /(a)\1[1]$ because I seem to recall that circa 5.8.3 or 5.8.4, code was specifically added to the regex engine to cause character classes that contained a single character to be optimised away to that character. This was done because explicitly because it makes the use of the construct as an escaping mechanism so useful and clear.

      Ie. /[f][r][e][d]/ becomes exactly equivalent to /fred/, including its runtime performance.

      Maybe someone out there has a reference to the change, I looked but could not find it.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        That optimization came with 5.10.

        From man perl5100delta:

        Single char char-classes treated as literals Classes of a single character are now treated the same as i +f the character had been used as a literal, meaning that code tha +t uses char-classes as an escaping mechanism will see a speedup. ( +Yves Orton)

        I still think it's /...(?:\1).../.

        • In math and Perl, when one wants to group operators, one uses parens. Seems natural to use them here too.
        • Enclosing in parens even has a precedent in Perl: /${prefix}_s/, /(?s:.)/
        • It's self-contained instead of requiring unrelated code to change. This solves /...\1$pat.../.
        • It's has no run-time effect (as shown below).
        $ perl -Mre=debug -E'qr/(a) \1 1/x' Compiling REx "(a) \1 1" Final program: 1: OPEN1 (3) 3: EXACT <a> (5) 5: CLOSE1 (7) 7: REF1 (9) 9: EXACT <1> (11) 11: END (0) anchored "a" at 0 floating "1" at 1..2147483647 (checking floating) mi +nlen 2 Freeing REx: "(a) \1 1" $ perl -Mre=debug -E'qr/(a)(?:\1)1/' Compiling REx "(a)(?:\1)1" Final program: 1: OPEN1 (3) 3: EXACT <a> (5) 5: CLOSE1 (7) 7: REF1 (9) 9: EXACT <1> (11) 11: END (0) anchored "a" at 0 floating "1" at 1..2147483647 (checking floating) mi +nlen 2 Freeing REx: "(a)(?:\1)1"

        Update: Me tired. Grammar bad. Fixed.