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.
Code
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
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Regular Expression TesterTest regular expressions against sample text with live match highlighting, capture group extraction, and flags support for global, case-insensitive, and multiline modes.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.