Member-only story

Reading and writing regular expressions for sane people

Photo by Andri from Pexels

Your regular expressions need love. Reviewers and future maintainers of your regular expressions need even more.

No matter how well you’ve mastered regex, regex is regex and is not designed with human-readability in mind. No matter how clear and obvious you think your regex is, in most cases it will be maintained by a developer who a) is not you and b) lacks context. Many years ago I developed a simple method for sanity checking regex with comments, and I’m constantly finding myself demonstrating its utility to new people.

There are some great guides out there, like this one, but what I’m proposing takes things a step or two further. It may take a minute or two of your time, but it almost invariably saves a lot more than it costs. I’m not even discussing flagrant abuse or performance considerations.

Traditional regex: the do-it-yourself pattern

The condescending regex. Here you’re left to your own devices. Thoughts and prayers.

Kind regex: intention…

--

--

Adam Fisher / fisher king (@therightstuff)
Adam Fisher / fisher king (@therightstuff)

Written by Adam Fisher / fisher king (@therightstuff)

Software developer and writer of words, currently producing a graphic novel adaptation of Shakespeare's Sonnets! See http://therightstuff.bio.link for details.

No responses yet