Accent in Regular Expression in Java

后端 未结 2 1097
天涯浪人
天涯浪人 2021-02-01 06:45

I\'d like to use Hibernate Validator to validate some columns. The problem, as I understand, is that the \\w marker in java doesn\'t accept letters with accents on them.

2条回答
  •  暖寄归人
    2021-02-01 07:14

    I had more luck with:

    \p{InCombiningDiacriticalMarks}+
    

    In java I use the following method:

    import java.text.Normalizer;
    import java.text.Normalizer.Form;
    
    public static String removeAccents(String text) {
        return text == null ? null :
            Normalizer.normalize(text, Form.NFD)
                .replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
    }
    

提交回复
热议问题