Regular Expressions

Inspired by this xkcd and the desire to purchase the associated t-shirt (and be truthful when wearing it), I've begun following the course on Learn Code The Hard Way on Regular Expressions.

The course uses a custom Python regex matcher (called regetron) and builds you up through a series of lessons, teaching you to translate regular expressions into regular English and back. I found his setup instructions quite unhelpful though so I thought I would include my own here in case anyone else struggled!

Note that I'm using Windows 8.1 on my laptop and haven't tested this anywhere else so your experience may differ!

  1. Install Python

    Header over to http://www.python.org/getit/ and download the appropriate installer. Make sure to change the default settings to add Python to your PATH variable.

  2. Install Pip

    According to what I could find online, Python ships without a package manager so we now need to install one - PIP. I found the instructions here to be the most helpful I could find. Download these 2 python files (ez_setup.py and get-pip.py) and then run python ez_setup.py and python get-pip.py from a command prompt in the directory you saved the files into.

    At this point, I found it helpful to add the Python scripts directory to my PATH too, meaning I can run pip from a command prompt anywhere. Go to This Computer, right click and choose Advanced System Settings, then Environment Variables. Find the one called PATH and add whatever your Python Directory is \Scripts (for me this was C:\Python33\Scripts).

  3. Install Regetron

    Now we should be able to install the custom python program we need. Run pip install regetron in a command window and it should install! To run the program, you can then type regetron and you should be all set up to follow lesson 1!

  4. Python 3

    If, like me, you knew very little to nothing before installing Python, you probably installed the latest version (at the time of writing 3.3), thinking that was the sensible thing to do. Apparently, there were a large number of breaking changes between Python 2.7 and Python 3 and so some older scripts need to be upgraded. Unfortunately, this includes regetron. If this is your situation, you have two choices:

    • Uninstall Python 3 and follow the steps above again with Python 2.7 (this is not what I chose to do)
    • Edit the regetron code and get it working with Python 3.

    I used the guide here to figure out some of the errors. I also ended up removing some sections which didn't seem to be needed or work on Windows. You can download what I came up with here. Once again, I'd like to stress that this works on my PC for me and may not work for you.

    To install, you'll need to replace the regetron file in your python Scripts folder (for me this was C:\Python33\Scripts) with mine, and also replace the regetron engine.py file which should be in your python lib folder (for me this was C:\Python33\lib\site-packages\regetron\engine.py) with mine.

Having got all of that working I was then able to start following the lessons! So far, I've definitely found his way of writing the regular expression out in plain English and breaking it down symbol by symbol really helps.

Hopefully I'll now be able to at least understand some of the regexes I copy and paste regularly (for example, those to match US zip codes) and I might even trust myself to write my own.

Piano playing. Whisky drinking. Programming, Book reading. Sport watching. Building cool things at StormIdeas. Blogging at http://miscampbell.com