Subscribe

Public Solving: Matching smudged names

✍️

How to match partially smudged names using regex in JavaScript

18 Dec, 2021 · 2 min read

Oh no, some packages fell off the sled, and the names are only partially readable.

You can find the puzzle here.

It’s up to us to predict which name is on each smudged package.

We received a list of all the children’s names and a list of gifts with smudged names.

Let’s think about the solution to help Santa as soon as possible.

Thinking about the solution

My initial thought is, great. We can use the filter method to filter the list of names with whatever name roughly matches the smudged name.

To do the rough matching, we can actually use Regex and not a super-advanced one, as you might think!

Finding the smudged names

Alright, let’s get right into it.

First of all, we need to import all the kids’ names.

import names from '../data/names.js';

Then we can return the array of names using the JavaScript filter method to find the good ones.

return names.filter((name) => {
  // Todo
});

Then inside this, we need to define a regex to match a part of the string.

Let’s take a look at what the smudging looks like:

// Some examples:

Fr#der##k
Jo#ann#
Patt#

For Patt#, we should get two potential hits: Patti, and Patty.

The cool part about this assignment is that it states a smudge is always one letter. And Regex comes with a great tool, the dot (.), which says: ”. matches any character (except for line terminators)“

So we can replace all # with ., and we should get far already.

return names.filter((name) => {
  const regex = new RegExp(smudgedName.replaceAll('#', '.'));
  return name.match(regex);
});

This uses the RegExp function, where inside, we replace all hashtags with dots. Then we return only if the name matches this regular expression.

And the results look very promising, but not perfect!

Remember Patt# it also matches: Patterson, which surely can’t be right as it’s too many characters!

We can fix this by adding a $ sign at the end of our regular expression. The $ stands for the end of the line.

Making our complete function look like this:

return names.filter((name) => {
  const regex = new RegExp(`${smudgedName.replaceAll('#', '.')}$`);
  return name.match(regex);
});

Let’s run the test and see what happens:

Test running and succeeding

There we go. We fixed it.

I would love to hear your approach to this solution or what you would change.

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Public Solving: Find the missing presents

3 Jan, 2022 · 2 min read

Public Solving: Find the missing presents

Public Solving: Caesar decipher in JavaScript

2 Jan, 2022 · 3 min read

Public Solving: Caesar decipher in JavaScript

Join 2099 devs and subscribe to my newsletter