Archive for December, 2006

Memorize (and Understand!) Regular Expressions in 20 Minutes Part 2

This is a continuation of Part 1, and now we’ll memorize the wild cards and repetitions in regular expressions.

So, we’ll just go through the line of expressions with a way to memorize each:

. - A period matches any character. Visualize a million grains of sand. Each of them is the same and matches the other exactly.

\w - Backslash w matches any alphanumeric character and “_”. Think of a wireless area network. It’s all lowercase, and and it can only use alphanumeric characters in the name.

\W - Backslash capital W matches any non-alphanumeric character. Think of George W Bush. He’s everything but any alphanumeric character. Nothing but wild cards, and there’s a capital W in his middle name.

\s - Backlash s matches a whitespace character. There’s plenty of space in outer space. Both lower case.

\S - Matches any non-whitespace character. Imagine a bunch of big S’s cramped into a small room. No space.

\d - Matches any digit character. Imagine a geeky little d who loves digits.
\D - Matches any non-digit character. Imagine a giant D bully that picks on anyone who’s a digit.

\t - Matches a tab. Shouldn’t you drink tea instead of Tab (A soft drink)?

\n - Matches a newline. Imagine an n going down a zip-line in New Guinea.
\r - Matches a return. Imagine an r on it’s way home from a long journey.

You follow any of these up with a wild card:

* - Matches a character 0 or more times. Imagine a star who’s died any become a black hole. It accepts anything.

+ - Matches a character 1 or more times. In order to add, you have to have at least 1.

? - Matches 1 or 0 times. Think of “To be or not to be?” There are to options. 1 or 0.

{n} - Matches n times. Imagine n n’s are trapped in n cages.

{n,} - Matches at least n times. Imagine n, and at least one other person trapped in cages.

{n,m} - Matches at least n times, but no more than m. N can break out until he reaches m’s cage.

So, for example:

$strung =~ m/S\{5}\/; # 5 non-whitespace characters?

$strang =~ m/\n{2}\/; # 2 newlines in a row?

See, using regular expressions is easy! It just looks difficult because you didn’t take the time to visualize easy ways to remember them. Write down the ways of memorization for yourself, and really visualize each of the above. You can really memorize all of the regular expressions in under 20 minutes when you’re focused.

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.

Close
E-mail It