3 ms·
Technically, for Perl you can use $ for regex backreferences. I don't remember which one is preferred, but I remember it frowning upon one of them.
by rfurmani 14y ago
Technically, for Perl you can use $ for regex backreferences. I don't remember which one is preferred, but I remember it frowning upon one of them.
- btilly 14y agoWhat you use depends on where you use it. Inside of a match, \1 refers to what was matched by the first parens. After the match $1 refers to what was matched. In a substitution, \1 is special cased to mean $1, but that is frowned on. Thus: /\b(\w+)\W+\1\b/ means "match repeated word". And: /\b(\w+)\W+$1\b/ means "match word preceeded by the word matched on your last match. Moving on: s/Hello (\w+)/Goodbye $!/ means "Replace Hello followed by a word with Goodbye followed by the same word." And finally: s/Hello (\w+)/Goodbye \1/ is special cased to mean the same thing, but is frowned upon.