Regular Expression Pushups

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:

  1. Find out if a certain string exists in as a substring within a document; and if so, where
  2. Replace the contents of a string within a document with something else
  3. Find three keys parts of a document and pull them out
  4. Find the word “foo” in a sentence but it cannot be “foobar”.
  5. Find a string that is a whole word only
  6. Find a string that is not a case-sensitive match
  7. Find the string foo or bar
  8. Match foo and goo (and so forth) but not boo without using these as words in your formula
  9. Find all strings that end in “oo” (3 character, and unlimited characters)
  10. Find any string that ends in “oo” but boo is not valid (REPEATS #8?  Or subtle difference?  I think I reversed them)
  11. See if a word matches that starts with “foo”.  Additionally, one that does not start with “foo”.
  12. See if a string matches that ends with “bar”.  Additionally, one that does not end with “bar”.
  13. Find a substring that begins with “foo” and ends with “bar”.
  14. Find the first and last word in a sentence
  15. Find the first character that is not a number or digit in a string
  16. Find the first number in a string (and last number)
  17. What is the text before the phrase “in the middle”?  What is the text that follows?
  18. Find  “abc” or “abcabc” and so forth in the sentence
  19. Find the string “abc” or “abcdef”
  20. Find the string “abc” or “abc1” or “abc11” and so forth
  21. Find all alphabetic words that end with the first “3”
  22. Find the word that starts with an alphabetic character and ends with 17
  23. Find the telephone number in the format 571-217-9451
  24. Find a number (without commas) that is at least 5 digits
  25. Find a number between 4 to 9 digits
  26. Find the word that matches a sequence of 5 instances where there is one to 3 numbers and a single character
  27. Find the string “abc” at the end of the string where there is a newline character
  28. Given a dollar figure, return the portion without the cents.
  29. Given string “#@%# 123bar 23bar 342 siojbar”, find the first word that does not have “bar” in it
  30. Find all the instances of numbers greater than 5 digits
  31. Find the number followed by the a space and word “bang” from “123 howdy 456 wow 789 bang”
  32. Do a greedy match (from “abc!def!ghi!” get the whole thing for .+!)
  33. Do a non-greedy match (from “abc!def!ghi!” get “the whole thing”abc!”” for .+!)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s