Search code examples
javaregexstringunicodenormalization

how to replace Latin unicode character to [a-z] characters


I'm trying to convert all Latin unicode Character into their [a-z] representations

ó --> o
í --> i

I can easily do one by one for example:

myString = myString.replaceAll("ó","o");

but since there are tons of variations, this approach is just impractical

Is there another way of doing it in Java? for example a regular Expression, or a utility library

USE CASE:

1- city names from another languages into english e.g.

Espírito Santo --> Espirito Santo,


Solution

  • This answer requires Java 1.6 or above, which added java.text.Normalizer.

        String normalized = Normalizer.normalize(input, Normalizer.Form.NFD);
        String accentRemoved = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
    

    Example:

    public class Main {
        public static void main(String[] args) {
            String input = "Árvíztűrő tükörfúrógép";
            System.out.println("Input: " + input);
            String normalized = Normalizer.normalize(input, Normalizer.Form.NFD);
            System.out.println("Normalized: " + normalized);
            String accentRemoved = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
            System.out.println("Result: " + accentRemoved);
        }
    }
    

    Result:

    Input: Árvíztűrő tükörfúrógép
    Result: Arvizturo tukorfurogep