Java Stack Trace Frame Regex for Java
/^\s+at ([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)+)\.([a-zA-Z_$<>][a-zA-Z0-9_$<>]*)\(([a-zA-Z_$][a-zA-Z0-9_$]*\.java|Native Method|Unknown Source)(?::([0-9]+))?\)$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching java stack trace frame, ported and verified for Java. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.
Java Implementation
// Java Stack Trace Frame
// ReDoS-safe | RegexVault — Dev & Systems > Log Parsing
import java.util.regex.Pattern;
public class JavaStackTraceFrameValidator {
private static final Pattern PATTERN =
Pattern.compile("^\\s+at ([a-zA-Z_$][a-zA-Z0-9_$]*(?:\\.[a-zA-Z_$][a-zA-Z0-9_$]*)+)\\.([a-zA-Z_$<>][a-zA-Z0-9_$<>]*)\\(([a-zA-Z_$][a-zA-Z0-9_$]*\\.java|Native Method|Unknown Source)(?::([0-9]+))?\\)$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate(" at com.example.MyClass.myMethod(MyClass.java:42)")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
at com.example.MyClass.myMethod(MyClass.java:42) | at com.example.Class.method(File.java:10) |
at java.lang.Thread.run(Thread.java:833) | com.example.Class.method(File.java:10) |
at com.example.App.<init>(App.java:10) | at bad format |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | — |
When to use this pattern
This pattern is drawn from the Dev & Systems > Log Parsing category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Lambda expressions and anonymous classes appear as ClassName$$Lambda$N or ClassName$1. The $ in class names must be in character classes.
Technical Notes
Groups: 1=FQCN, 2=method name, 3=source file or 'Native Method'/'Unknown Source', 4=line number. Leading whitespace is required. <init> and <clinit> are valid method names.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern