Skip to content

Instantly share code, notes, and snippets.

@nurv
Created April 24, 2014 09:17
Show Gist options
  • Save nurv/11247778 to your computer and use it in GitHub Desktop.
Save nurv/11247778 to your computer and use it in GitHub Desktop.
public static String slugify(String name) {
Pattern NONLATIN = Pattern.compile("[^\\w-]");
Pattern WHITESPACE = Pattern.compile("[\\s]");
name = name.trim();
name = Normalizer.normalize(name, Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
String nowhitespace = WHITESPACE.matcher(name).replaceAll("-");
String normalized = Normalizer.normalize(nowhitespace, Form.NFD);
String slug = NONLATIN.matcher(normalized).replaceAll("");
name = slug.toLowerCase(Locale.ENGLISH);
return name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment