DEV Community

Cover image for Regex 101: A primer
Mojca Rojko
Mojca Rojko

Posted on

Regex 101: A primer

Let's examine a regular expression, the kind you could get to fill out live at a technical interview for a variety of languages and frameworks. Why? Because it shows your logic perfectly and it shouldn't take you more than 10 minutes to complete, even if you're rusty on the regular expression logic. You're allowed to use https://regex101.com, which has a wonderful help section which you can use.

The instructions

Here's an unfinished regular expression:
(?<name>.*)$
(see and open https://regex101.com/r/hhwiRl/2)

This is your test string:
2-13_oops-i-did-it-again_britney-spears.mp3
3-13_toxic_britney-spears.vaw
hold-it-against-me_britney-spears.flac
unknown.mp3

There is already a baseline provided, I’d like you to finish this regex. Imagine the test input as a text file containing multiple lines of songs on different albums. For each line, we’d like to extract the edition (so which song on the album it is), the song name, the artist name and the extension. The name is the only thing required. Imagine this as extracted metadata in a player like Winamp. The different parts of the filename are separated by underscores and spaces are mimicked with dashes. It is OK if the parts contain the dashes, as you will not be able to replace them within this regex. All the help you need is in the bottom right corner of the webpage so I’d encourage you to use it as nobody knows all of the delimiters by heart.

The solution

What you'd be expected to come up with, is this:
(?<edition>\d{1,8}-\d{1,8})?_?(?<name>[^_.]+)_?(?<artist>[^_.]+)?.(?<extension>[\S]{1,8})?$
(see https://regex101.com/r/3qW52u/1)

First thing you'll notice is the usage of named groups. In javascript, we'd extract the named groups like this:

    const {
      groups: { edition, name, artist, extension },
    } = /(?<edition>\d{1,8}-\d{1,8})?_?(?<name>[^_.]+)_?(?<artist>[^_.]+)?.(?<extension>[\S]{1,8})?$/.exec(
      filename
    );
Enter fullscreen mode Exit fullscreen mode

For the rest, you can see a very nice explanation that regex101 came up with for our regex:

explanation

And here is a visualisation of one of our matches:

visualisation

Voila!

Top comments (0)