Regex saved me 57 minutes

Hey there, 👋

How is your Python going?

In this Mathspp Insider 🐍🚀 email we’ll talk about how basic regex skills can be so helpful outside of programming!

This email at a glance

It’s been a minute since I last wrote to you!
Sorry about that!

December was hectic with a very successful 2nd cohort of the Problem-Solving Bootcamp and I decided to take a much needed break during Christmas/New Year.

In this email I’ll

A gift for my uncle

A mockup of an ebook called puzzles, riddles, & problems.

I have a degree in maths and if I had to trace back the path I took to get there, I think it all started with my uncle challenging me with logic puzzles and riddles.

When I started my blog, I started sharing some of those puzzles, riddles, and problems, and I’d read the emails from people sending me their solutions.

This Christmas I decided to compile the 64 problems I’ve shared on my blog over the years into an ebook.

I took the ebook, I printed all the 64 problems, 64 solutions, and some hints for some of the problems, and I made a little book out of that to gift my uncle.

My uncle liked it :)

I don’t have a physical book for you, but I made the ebook available to everyone in the last couple of days.

By the way, if you know any interesting puzzles, riddles, or problems that are related to logic, maths, or programming, reply to this email and share them with me!

I’m always looking for more problems to share on my blog.

Let me tell you how I used the module re to save me around 57 minutes of manual work.

Find + replace, manually…

To create the puzzles ebook I mentioned above, I took the problems from my blog and laid them out in a markdown file, which ended up with this structure:

# Problems

## Dancing triangle

...

## Bag full of numbers

...

## Quarrel in the Shire

...

Those are the titles of the first three problems.
Like I said, there’s 64 of them.

After some time I decided I wanted to number the problems to make navigation easier.

Thus, I wanted to change my file into something like this:

# Problems

## 01 – Dancing triangle

...

## 02 – Bag full of numbers

...

## 03 – Quarrel in the Shire

...

I started doing this manually and after 5 titles I was already tired and bummed.

I’d need to do this for the 64 problems and the 64 solutions!

Looking for the titles with regex

As quickly as I felt desperation take over me, I felt relief:

“regex will save me”, I thought.

I stopped for a second and wrote down this regex pattern:

^## (.*)$

If you don’t know regex, the pattern above is unreadable, but it says more or less the following:

  • I want my matches to start at the end of the line ^

  • I want to start by matching two octothorps and a space ##

  • then I’ll match as match as many (*) arbitrary characters (.) as possible

  • until the end of the line $

Go to regex101 (an online regex playground), paste the regular expression ^## (.*)$ in the top, middle bar, and copy and paste the first version of the # Problems markdown in the big text area.

You’ll see the headings get highlighted.

Dynamic string replacements

Now that I know how to find headings, I need to replace them with the same heading but with an integer before it.

The only problem?

The integer is supposed to change for each replacement.

Thankfully, the function re.sub from the module re can accept a function that builds dynamic replacements.

So, I can define a function replacer that will be used by re.sub to build numbered headings:

counter = 0
def replacer(match):
    global counter
    counter += 1
    return f"## {counter:02} – {match.group(1)}"

Finally, with a single magic call re.sub("^## (.*)$", replacer, markdown_string, flags=re.MULTILINE), I managed to get the 128 replacements done in about 5 minutes!

In the process, I must’ve saved some 57 minutes or so!

Read the full story

ICYMI

One or two days ago I released an ebook I’ve been sitting on for quite a while, now.
It’s an ebook with 64 logic puzzles, riddles, and problems.

I’ve written a couple of articles in December (1) and this year (4) that you might enjoy:

These are all fairly short articles, apart from the introduction to pandas and matplotlib.

🐍🚀 How was this email?

Login or Subscribe to participate in polls.