Regex CANON_EQ Matches Decomposed Accents

Pattern.CANON_EQ matches precomposed and decomposed accents as equal, so café with a combining mark still finds the precomposed pattern.

JavaForm Validation

Code

snippet
import java.util.regex.Pattern;

public class AccentMatcher {

    public static void main(String[] args) {
        String decomposed = "cafe\u0301"; // e + combining acute: 分解形态的 accents
        String precomposed = "caf\u00e9"; // 预组合 é

        // 缺省时 plain regex 匹配不到分解形态;CANON_EQ 按规范等价比较
        Pattern plain = Pattern.compile(precomposed);
        Pattern canonical = Pattern.compile(precomposed, Pattern.CANON_EQ);

        System.out.println("plain matches decomposed: " + plain.matcher(decomposed).find());
        System.out.println("canonical matches decomposed: " + canonical.matcher(decomposed).find());
        System.out.println("plain matches precomposed: " + plain.matcher(precomposed).find());
    }
}

Step-by-step behavior

The moving parts are the class AccentMatcher and the function main.

What to watch for

  • The toolchain by itself is enough — dependency resolution never happens.

Using it

The primary signature here is public static void main(String[] args)

The demo output is labeled (plain matches decomposed:, canonical matches decomposed:, plain matches precomposed:), so each printed line can be matched to the operation producing it.

Key statements

String decomposed = "cafe\u0301"; // e + combining acute: 分解形态的 accents
String precomposed = "caf\u00e9"; // 预组合 é
// 缺省时 plain regex 匹配不到分解形态;CANON_EQ 按规范等价比较
Pattern plain = Pattern.compile(precomposed);
Pattern canonical = Pattern.compile(precomposed, Pattern.CANON_EQ);

Interactive Tools for This Topic

FAQ

What does Regex CANON_EQ Matches Decomposed Accents print when run?
Invoke the file as-is — the `plain matches decomposed:`, `canonical matches decomposed:`, `plain matches precomposed:` prefixes identify which step printed each line.