Memorize (and Understand!) Regular Expressions in 20 Minutes
I’ve been reading the classic book on memorization, The Memory Book, and it’s really a complete memorization system. By associating new ideas with something you already know in a ridiculous way, you can memorize massive amounts of information at once.
We’ll start by memorizing the classically difficult to remember (and understand) regular expressions in Perl.
For those of you that don’t know, regular expressions allow us to do cool stuff with strings like change capitalization with a single line of code.
=~
This is the equals tilde character. It takes a string and searches through it to see if it has what we’re looking for. To remember this we could break down “equals tilde” into something that sounds similar. Ecstasy quells tiles (E-quells tiles). Imagine a bunch of tiles at a rave taking ecstasy. They’re all searching for something true. The meaning of life, maybe. Either way they’re searching for some sort of truth.
$mylife =~ m/so cool/;
!~
Not tilde. Same as =~, except returns true if the expression doesn’t match. Imagine a bunch of naughty tiles who hate ravers. They don’t think we should search for truths, but rather search for falsehoods.
$life !~ m/meaningful/;
/
Slash. The delimiter in expressions. Easy to remember. Just imagine cutting the expression with a butter knife.
$forrestgump =~ m/Bubba/;
m
“m”, the match operator. Returns true if there’s a match in the expression to the immediate right of it. Just think of m as being a highly paid matchmaker living in Manhattan with a million dollar loft.
$matchmaker =~ /match/;
^
Carat, the beginning of a new line character. If it’s found between the slashes, it means we’re looking for a new line. Just imagine Elmer Fudd holding a carat on a fishing line over Bug’s home
$joke =~ m/^Not funny/;
$The end of line character. Think of money as being the end of all life. Example:
$money =~ m/the end$/;
i
The case insensitivity character. I am insensitive to avocado farmers. Example:
$strung =~ m/mYSpaCe/i; # returns true if the idiot wrote Myspace in weird caps
tr
The translate character. Takes one character and replaces it with another. Think of a drunk, one armed Tony Robbins translator to remember it. He sure is a character.
$sweet =~ tr/[a-z]/[A-Z]; # replace lower case characters with upper
s
Substitute string. Substitutes an entire string with another. Simon says substitute.
$president =~ s/George Bush/Reptoid/; # Replaces the president with a reptoid
[]
Brackets, organize by character classes. Takes in and searches character by character. Think of an actor breaking out of character and crying because his trailer is dirty. Usage:
$sweet =~ tr/[a-z]/[A-Z]/;
That’s it for part one. On to part two, where we learn about wild cards.
Related Posts:
December 25th, 2006 21:35
[...] « Memorize (and Understand!) Regular Expressions in 20 Minutes [...]
December 26th, 2006 17:53
What about positive and negative lookahead and lookbehind, and capturing?
December 30th, 2006 07:21
“^” doesn’t match at a newline, it matches at the beginning of string.
Also, you don’t need the [] in those tr/// ops… a simple dash always indicates a range. In fact, what you’re doing is translating a left bracket into a left bracket… completely pointless.
October 24th, 2007 20:29
All about ford cars and parts ford oaklandford oakland
December 15th, 2007 11:53
very interesting, but I don’t agree with you
Idetrorce
February 19th, 2008 10:16
I impersonate Heather’s experienced, reasonated dishes celebrity jeopardy anal bum cover the celebrity movie sex scenes of the ham bag into my ass.
February 24th, 2008 13:24
Well, neighborhood of. It’s explosively quite like a spell, but that’s a brutal yeally analogy.