Extract Values Between Delimiters

Pull values out of delimited log fields with indexOf and substring instead of regex, and return null for missing keys so callers decide what absence means.

JavaData Fetching

Code

snippet
public class LogFieldExtractor {

    // Lines look like [2026-11-18 09:15:02] level=INFO module=auth message="login ok"
    // Extract the value between key= and the next space: indexOf locates, substring cuts
    static String extractBetween(String line, String key) {
        int keyAt = line.indexOf(key + "=");
        if (keyAt < 0) {
            return null; // a missing key is the caller's semantic decision
        }
        int valueAt = keyAt + key.length() + 1;
        int end = line.indexOf(' ', valueAt);
        if (end < 0) {
            end = line.length();
        }
        return line.substring(valueAt, end);
    }

    public static void main(String[] args) {
        String line = "[2026-11-18 09:15:02] level=INFO module=auth message=\"login ok\"";
        System.out.println("level: " + extractBetween(line, "level"));
        System.out.println("module: " + extractBetween(line, "module"));
        System.out.println("trace: " + extractBetween(line, "trace"));

        // Quoted values containing spaces need a different terminator: find the opening quote, take the rest of the line, then strip the quote
        int valueAt = line.indexOf("message=\"") + "message=\"".length();
        System.out.println("message: " + line.substring(valueAt, line.length() - 1));
    }
}

Inside the snippet

The snippet is organized around the class LogFieldExtractor and the functions extractBetween and main.

What to watch for

  • Self-sufficient by design — there is no registry and no lockfile entry.

Using it

The main signature is written as static String extractBetween(String line, String key)

Look for the level:, module:, trace: prefixes as the file executes; they connect every printed line to the step behind it.

Execute the file and main walks through extractBetween, in that order.

Key statements

return null; // a missing key is the caller's semantic decision
}
return line.substring(valueAt, end);

FAQ

What does Extract Values Between Delimiters print when run?
The script runs standalone; its 4 labeled outputs include `level:`, `module:`, `trace:`.