I find regular expressions obtuse, maddening, and at the same time, strangely compelling. For those who don’t know, “regular expressions” (usually referred to as “regex”) is a common way for most computer programs to search text for particular patterns of characters, like for example a date or an address. Regex gives you access to atoms that represent individual character (“a”, “6”, “#”, etc…) or general character types (digits, symbols, uppercase…), as well as a concise grammar defining how these characters appear. Regex is a set of small blocks with specific rules that define how to put all these blocks together to represent something bigger.
A quick example
How to match a date like 2014/12/24?
Digits are represented like this: \d so the following regex will represent any character from 0 to 9:
\d
What if you are looking for a series of 4 digits? You could do this:
\d\d\d\d
But it can get a little heavy if you are looking for 200 digits, so you can use the following notation:
\d{4}
Okay, so now you have matched the first 4 digits, how about matching the “/” character? In this case, since it’s just a specific character, just go ahead and type it in:
\d{4}/
Then match 2 digits, another “/” and two more digits:
\d{4}/\d{2}/\d{2}
It’s a mental puzzle that requires you to visualize how patterns can be broken down into concise nuggets of representative abstraction.
Regulex
The following web site creates a diagram out of a regex expression to give you a useful visual graphic depiction of the logic.
Regexpress
This next one uses similar visuals but allows you to build the expression by stringing along icons that represent the patterns you are looking for.
Regviz
Here’s another one that doesn’t use fancy charts and graphics but it does allow you yo see the matches in real time with text you specify.