Handy trick: Swapping two substrings in VS Code with regular expressions

Posted on 11 February 2020. Tagged with vs code, regex

I've always been a little bit scared of regular expressions - it always feels like the struggle to learn how to use them will outweigh the benefits. Usually I'd rather just spend a bit longer manually editing code. But today I learned a handy trick!

I had a list of book recommendations from Frank Chimero's blog that I wanted to add to my Roam notes. They were all formatted as <Title> by <Author>. I wanted them to be formatted as [[<Author> - <Title>]].

In VS Code, the way to go is to use find and replace:

Find:       (*.) by (*.)
Replace:    [[$2 - $1]]

Pretty simple! This is how it works:

If you're learning Ruby, Russ Olsen's book Eloquent Ruby has a great chapter on regular expressions. It makes it much less intimidating!

I don't have comments on my blog yet. Tell me what you think on Twitter.