Matcher hitEnd Detects Truncated Input
Distinguish "no match" from "not enough input yet" with hitEnd so streaming validators can wait for more data instead of rejecting early.
Code
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StreamingEmailCheck {
static final Pattern EMAIL = Pattern.compile("[\\w.]+@[\\w]+\\.[a-z]{2,}");
// 三态判定:拒绝 / 匹配 / 还需要更多输入
static String judge(Matcher m) {
if (m.matches()) {
return "complete: " + m.group();
}
if (m.hitEnd()) {
// 输入在可能影响匹配的位置结束:等下一批再判
return "partial, waiting for more input";
}
return "definitely invalid";
}
public static void main(String[] args) {
String[] inputs = {"ada@", "ada@lovel", "[email protected]", "ada!@"};
for (String input : inputs) {
System.out.println(input + " -> " + judge(EMAIL.matcher(input)));
}
}
}The flow at a glance
All of the logic lives in the class StreamingEmailCheck and the functions judge and main.
The file repeats itself only inside for (String input : inputs).
What to watch for
- Dependency-free by construction — no registry, no lockfile entry.
Using it
Its signature looks like static String judge(Matcher m)
main sequences judge; executing the file runs that flow once.
Lines that do the work
return "partial, waiting for more input";
return "complete: " + m.group();
return "definitely invalid";
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.URL Pattern MatcherTest Express-style URL patterns with :param and wildcard segments against concrete paths and extract the captured parameter values.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.
FAQ
- How do you run Matcher hitEnd Detects Truncated Input?
- Direct execution is enough; the script has no external requirements.
Report a problem with this exampleOpens a pre-filled issue on GitHub.