Editor

Make a URL Slug

#283 · Regular expressions · Intermediate · 65 XP · about 12 min

Blogs turn a post's title into a short, readable piece of its web address called a slug: "Crème Brûlée: 10 Tips!" becomes creme-brulee-10-tips. Write slugify(title) that builds a slug with these steps, in this order:

  • Remove accents. unicodedata.normalize('NFKD', title) splits an accented letter such as é into a plain letter followed by a separate accent mark, and unicodedata.combining(ch) is not 0 for those marks, so drop every character for which it is not 0.
  • Make every letter lowercase.
  • Delete apostrophes, both ' and ’, so "Don't" becomes "dont".
  • Replace every run of characters that are not a to z or 0 to 9 with a single -.
  • Remove any - from the start and the end.

If nothing is left, the slug is the empty string.

Examples

slugify('Hello, World!')               # returns 'hello-world'
slugify('  Crème Brûlée: 10 Tips! ')   # returns 'creme-brulee-10-tips'
slugify("Don't Stop -- Café Edition")  # returns 'dont-stop-cafe-edition'
slugify('!!!')                         # returns ''

Constraints

  • title has at most 100,000 characters.
  • Every letter in a title is an English letter, with or without accent marks.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.