Wednesday, November 27, 2013

How "junior" developers can become Regex wizards

You need a Regular Expression to validate a phone number or a user's email address, so what do you do? Go onto StackOverflow and copy someone else's code and paste it into your code and be done with it? Well that may work, and if you are in an absolute pinch that can work, but what have  YOU personally really learned? Zilch.

At my job as a "junior developer" I am starting to have to use Regex (Regular Expressions) quite a bit to validate/check for certain things. I could become a "Googling wizard" but I'd rather expend the energy and effort now to become a "Regex wizard" instead. The truth is, Regular Expressions seems
daunting and intimidating, and they may be at as they get more and more complicated, however I think Regex are just different and you need to slow down and think of them as symbols and not try to rush through.

With that spirit in mind I recorded myself doing a basic Regex date string, I did it "cold turkey", so as to be authentic, and real. I did make one mistake and then fixed it, so you can see I'm quite new to this and human :-)  The point is not how much I suck, the point is to go ahead and just try to make your own Regular Expression before just "Googling the answer". I want to be a good developer one day, and I think as a young developer we should put in the extra time to try and really understand something and not just always do what is the quickest.










If you are new to Regex, www.rubular.com is your friend. Put test strings in the box on the site and then look at the Regex "cheat sheet" on the bottom of the page when you get stuck or need a refresher. To the right you will see a blue bar that tells you if you are matching the 'test string' correctly or not which makes it easy to tell if the Regex you are making is actually correct or not.

The other thing is, DON'T RUSH, if you can't take a few minutes at work to fiddle with Regular Expressions, then make sure while eating some turkey and watching the game at Thanksgiving tomorrow that you pull out some random strings to try and match, I find it's actually really fun and not stressful if you don't pigeon hole yourself into a time crunch.

I always have my "mentor" developer at work double check my work when in doubt. NEVER put something that you are unsure of into production. Certainly on your own projects and stuff you should be able to take 5 - 10 minutes to have fun with some Regular Expressions.

Keep coding!

7 comments:

  1. I've been using regular expressions for a long time now, but hadn't seen rubular yet - thanks!

    I just blogged today about how to use regular expressions with SQLite.

    http://www.ashleyit.com/blogs/brentashley/2013/11/27/using-regular-expressions-with-sqlite/

    ReplyDelete
  2. I use debuggex.com

    It's considerably better then rubular, as it actually generates a flow-graph of the regex's behavior on-the-fly, in addition to allowing you to test your regex.

    ReplyDelete
    Replies
    1. That's really neat! I personally prefer http://gskinner.com/RegExr/ since the interface is so clean. But the flow-graph is pretty convenient!

      Delete
  3. From my experience, it really helps to learn about NFA's. It makes regex writing seem less like "wizardry". Also, knowing some basic automata theory provides a nice tool for visualizing how a particular regex will work.

    ReplyDelete
  4. This is a great tool. I also like http://www.regexper.com/

    ReplyDelete
  5. I'd recommend a work pattern of building the regexp and then doing the search for it that you mentioned at the beginning of the post.

    This way it's good any which way for a self-learner - you'll either get to validate what you did is right, spot something you missed or learn a different way of writing the same regexp.

    ReplyDelete
  6. Thanks guys!...wow half of those sites I didn't even know about till I looked them up just now. Thanks for reading :-)

    ReplyDelete