Skip to main content

What part of "$&" don't you understand?

Perl has some awfully handy stuff, even when it's in Ruby.

The Pragmatic Programmers start their Regular Expression chapter in the Pickaxe by discussing $& (the last regular expression match), and $', and $` (the strings immediately before and after the last RE match). Awfully convenient stuff, borrowed directly from Perl. At the end of the chapter they apologize:

"Having said all this, we have to 'fess up. Andy and Dave normally use the $-variables rather than worrying about MatchData objects. For everyday use, they just end up being more convenient. Sometimes we just can't help being pragmatic."

I wrote a little Ruby utility script recently to handle a bookkeeping chore, that does some screen-scraping and Excel-file-reading. About forty lines. $& figured prominently.

That's not so offensive, is it?