Regular Expressions and Transforming Reality

Posted

Back in 2021, I touched on ways that ‘work’ can resemble ‘play’, and I made a few notes to myself about specific examples I should write about sometime. It’s been a few years, but I finally revisited those notes.

Back then I quoted a post on a Montessori teaching blog, which could just as well have been a blog post on knowledge work:

In the broadest sense, play is always a transformation of reality in the service of the self.

Regular Expressions can teach you how to play

To say I know regular expressions well is to say I only know them when I work with them. If you were to ask me for the syntax of a negative look-behind zero-length assertion, I’d provide the general shape of one. And even then, it would depend on the specific language implementation we’re talking about.

But if you ask me more generally how to handle all the cases matching a piece of text, I’d advise you to just go and try it. Stop talking about it; just do it. Even if you get it wrong on your first pass, you’ll accomplish something by having played.

That Time I Paired On That Thing…

Working with a junior developer long ago was the first time I realized that my method of writing regular expressions resembles a form of play.

Here’s what I think he saw:

  1. I start with a simple match1 to get something that works.
  2. If there are any non-trivial capture groups, I open up a REPL or a site like Rubular to try it out.
  3. I read the expression so far, break it, and re-write it slightly differently.
  4. Make it better, where “better” depends on how we feel about it at any given moment until we get bored and move on.2

An Example

The following isn’t the regex mentioned above, but it’s an example of a regex I wrote to match “invalid” addresses used in a function to validate URLs:

  INVALID_ADDRESSES = %r{
    ^\w+://                          # any scheme
    (
      localhost                      # localhost - case insensitive
      |
      127(?:\.[0-9]+){0,2}\.[0-9]+   # ipv4 loopback addresses
      |
      (?:0*:)_*?::?0*_1              # ipv6 loopback addresses
    ).*
  }ix

Is it perfect? Is it usable in all contexts? No, but it solved the problem at hand.

Does it need to be a multi-line expression? Absolutely not. Do the comments need to be there? No. But that allowed me to document it, so I spend fewer cycles remembering how it works, and it can make sense to the next developer.

Maybe you play this game differently from me. That’s what’s so powerful about regular expressions as a playground (no pun intended) for talking about work can be a form of play.

The results may look the same, but a play-focused mindset has lasting effects on engagement, energy, and higher morale. We strive to develop these in ourselves and our teams, and we should encourage more of it.

“A transformation of reality in the service of the self”

Players are free agents. They not only choose freely to play the game or not, but they also choose how to play it. They must follow the rules, but within the guidelines of the rules each move must be their own …

It is not surprising, therefore, that workers who are free to make their own on-the-job decisions are much more likely to experience their work as play than are those who do not have such freedom.

– “Play Makes Us Human” (Psychology Today)


  1. Sometimes that’s all you need. ↩︎

  2. Some people like to think that emotions have nothing to do with technical work, but our confidence in a solution has much to do with our feelings toward it. ↩︎

Tags: