Skip to content

Instantly share code, notes, and snippets.

@gary-liguoliang
Last active May 23, 2020 16:20
Show Gist options
  • Save gary-liguoliang/ba951765b5b00388f5b25398239668f4 to your computer and use it in GitHub Desktop.
Save gary-liguoliang/ba951765b5b00388f5b25398239668f4 to your computer and use it in GitHub Desktop.
slugify strings with pure regex and python
import re
def slugify(s: str) -> str:
s_after_basic_replacement = re.sub("[^a-zA-Z0-9]", "-", s)
s_with_no_continues_dash = re.sub("[-]+", "-", s_after_basic_replacement)
s_with_no_ending_dash = re.sub("-$", "", s_with_no_continues_dash)
return s_with_no_ending_dash
if __name__ == '__main__':
print(slugify("is this a good title? "))
# is-this-a-good-title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment