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.

JavaForm Validation

Code

snippet
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

FAQ

How do you run Matcher hitEnd Detects Truncated Input?
Direct execution is enough; the script has no external requirements.