Justin Duke

A regular expression to detect Markdown ordered lists

I’m in the process of converting all of Barback’s recipes’ into directions that have ordered lists.

The English Garden is a good example of a before and after.

Before:

Muddle cucumber, lime, syrup and rose water. Next, add gin and st germain and shake with ice.  Double strain over ice and top with tonic. Garnish with cucumber.

After:

1. Muddle cucumber, lime, syrup and rose water.
2. Add gin and st germain.
3. Shake with ice.
4. Double strain over ice.
5. Top with tonic.
6. Garnish with cucumber.

It’s clearer and allows for nicer styling client-side on the iOS app as well!

However, I need to convert like 200 recipes to this format and I wanted a way of keeping track of my progress via a django-admin filter. Still, I needed a regular expression to actually pass in, so I started to tinker with one (huge thanks to Regex 101 for the help.)

I ended up with this:

‘[0-9]+.(.*)\n’

It’s actually fairly simple. Breaking it down:

  • [0-9]+ says we want a number
  • \. says we want a period after that number
  • (.*) says we want pretty much anything …
  • but \n says it’s gotta end with a newline.

This has worked awesome for my case, though it may not be exhaustive! Lemme know if you come up with a better one, but this should work for your purposes.

Liked this post? You should subscribe to my newsletter and follow me on Twitter.

(I've got an RSS feed, too, if you'd prefer.)