Regular expressions were not my strong suit, but I felt it was important to master them (or at least reach competency). So, on a flight back from Walt Disney World last summer, I studied them from one of my Ruby books and summarized them in detail in Evernote (to refer back to them). I thought I had a good enough understanding where I could refer back to my Evernote reference material and quickly put it together – I was wrong.
Writing a Rails app I have been working on for fun, it took longer for me to put a working regular expression together than I expected. I had studied, summarized and even written a few short programs for regular expressions in Ruby. However, what I hadn’t done was to go from the other direction – take a domain problem and map it back to a regular expression. That’s when I came up with “Regular Expression Pushups”.
When I was younger, I used to be able to do a massive amount of push-ups. Rather than continuing to increase the count, I tried to do them more quickly. Similarly, I reviewed my notes and created thirty-three problems that would use the underlying techniques. I would do these as quickly as possible, and then later change the questions a little to see if I could do them more quickly. The idea was to better form the “regular expression neural networks” in my brain
I’ve gone through one round and have seen a huge improvement. I expect the second round will go a lot faster than the first (how could it not)?!
Here are the ones I put together:
- Find out if a certain string exists in as a substring within a document; and if so, where
- Replace the contents of a string within a document with something else
- Find three keys parts of a document and pull them out
- Find the word “foo” in a sentence but it cannot be “foobar”.
- Find a string that is a whole word only
- Find a string that is not a case-sensitive match
- Find the string foo or bar
- Match foo and goo (and so forth) but not boo without using these as words in your formula
- Find all strings that end in “oo” (3 character, and unlimited characters)
- Find any string that ends in “oo” but boo is not valid (REPEATS #8? Or subtle difference? I think I reversed them)
- See if a word matches that starts with “foo”. Additionally, one that does not start with “foo”.
- See if a string matches that ends with “bar”. Additionally, one that does not end with “bar”.
- Find a substring that begins with “foo” and ends with “bar”.
- Find the first and last word in a sentence
- Find the first character that is not a number or digit in a string
- Find the first number in a string (and last number)
- What is the text before the phrase “in the middle”? What is the text that follows?
- Find “abc” or “abcabc” and so forth in the sentence
- Find the string “abc” or “abcdef”
- Find the string “abc” or “abc1″ or “abc11″ and so forth
- Find all alphabetic words that end with the first “3″
- Find the word that starts with an alphabetic character and ends with 17
- Find the telephone number in the format 571-217-9451
- Find a number (without commas) that is at least 5 digits
- Find a number between 4 to 9 digits
- Find the word that matches a sequence of 5 instances where there is one to 3 numbers and a single character
- Find the string “abc” at the end of the string where there is a newline character
- Given a dollar figure, return the portion without the cents.
- Given string “#@%# 123bar 23bar 342 siojbar”, find the first word that does not have “bar” in it
- Find all the instances of numbers greater than 5 digits
- Find the number followed by the a space and word “bang” from “123 howdy 456 wow 789 bang”
- Do a greedy match (from “abc!def!ghi!” get the whole thing for .+!)
- Do a non-greedy match (from “abc!def!ghi!” get “the whole thing”abc!”" for .+!)