Regix diacritic

I have the following regex:

String regExpression = "^[a-zA-Z0-9+,. '-]{1,"+maxCharacters+"}$";

which works great for me, except that it doesn't allow the use of UTF-8 diacritics (Ă ă Â Î î Ş ş Ţ ţ).

I only need my current regular expression to accept diacritics in it, other than what it already does.

Any help is appreciated. Thank.

+5
source share
1 answer

You need to learn the POSIX character classes to catch them. Unfortunately, Java Regex does not support POSIX language classes, but it may \p{Graph} A visible character: [\p{Alnum}\p{Punct}]or \p{Print} A printable character: [\p{Graph}\x20]will be consistent.

The best match suggested by Sorin is possibly \p{L}(Letter).

import java.util.regex.Pattern;

public class Regexer {

    public static void main(String[] args) {
        int maxCharacters = 100;
        String data = "Ă ă Â â Î î Ș ș Ț ț";
        String pattern = "^[\\p{L}0-9+,. '-]{1," + maxCharacters + "}$";

        Pattern p = Pattern.compile(pattern);

        if (p.matcher(data).matches()) {
            System.out.println("Hit");
        } else {
            System.out.println("No");
        }

    }
}

This works for me.

+6
source

All Articles