PatternSyntaxException Debug Index Description
PatternSyntaxException reports the failing index and a description, so a regex built from user input can reject bad patterns without guessing.
Code
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class RegexValidator {
// 用户输入拼出来的正则可能非法:PatternSyntaxException 自带 debug 用的索引与描述
static String probe(String rawToken) {
try {
Pattern.compile("(" + rawToken + ")");
return "ok";
} catch (PatternSyntaxException bad) {
// getIndex 定位非法字符,getDescription 说明错误类别
return "invalid at " + bad.getIndex() + ": " + bad.getDescription();
}
}
public static void main(String[] args) {
System.out.println(probe("alpha"));
System.out.println(probe("[alpha"));
System.out.println(probe("*zip"));
}
}The flow of the file
PatternSyntaxException Debug Index Description is built around the class RegexValidator and the functions probe and main.
Watch out for
- The project gains nothing new before this runs.
Using it
The signature at the center: static String probe(String rawToken)
main is the driver: it invokes probe before returning.
Key statements
return "invalid at " + bad.getIndex() + ": " + bad.getDescription();
return "ok";
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
- How do you run PatternSyntaxException Debug Index Description?
- The runtime alone is enough: execute the file as a complete program.
Report a problem with this exampleOpens a pre-filled issue on GitHub.