Chris Mattern said:
greendogday said:
It's just a cut down version of a problem, to show where the problem
lies.
The original had a match for either a double or single quote.
I don't understand that. I am backreferencing a match - the double
quote.
No, you aren't. You only think you are. \[1-9] is only "special"
in the second part of a search and replace, and can only refer to
No, that's wrong too.
The one-digit backreferences *are* a replacement for $1 .. $9 in a
regex (and the regex part of a s///) where $1 .. $9 can't be used.
Using them on the replacement side of a s/// works, but is considered
bad style.
They are match-time interpolated in the regex, but only in the parts
that are literal matching text. They are not interpolated in character
classes, and neither in {,}-quantifiers and probably a lot more places
that aren't matched literally. The non-interpolation in character
classes was the source of the confusion.
BTW, the existence of backreferences is what makes computer-style
regexes fundamentally different from their mathematical model. In
mathematics a "regular expression" disallows backreferences. That
limits the set of languages they describe in (mathematically)
interesting ways.
the first part of the same S&R. In a simple match, it doesn't mean
anything. It is a literal "1", which you escaped with a backslash,
making it still a literal "1".
Well, no. Here is a regex
/^(.)\1*$/
that uses a backreference to match all strings that are a repetition
of a single character, no matter which. This is something mathematicians
like to prove regexes cannot do.
print "'$_': ", /^(.)\1*$/ ? 'yes' : 'no', "\n" for
'', qw( a ab aaaa aaab XXX ;;;;; ;;;:;
;
Anno