Categories
Coding

Useful Regular Expressions, Part 2

Some useful regexes here that I have used recently here, saved in case I ever lose them (especially the last two). Firstly, an expression that parses NTLM credentials from HTTP BASIC authentication responses. Note the non-capturing groups ?: to keep indexing simple.

/* DOMAIN\\user(:pass)? or DOMAIN/user(:pass)? */
private static final Pattern CREDENTIALS_PATT = Pattern.compile("(?:(\\w+)(?:\\\\{1,2}|/))?(\\w+)(?::(\\w+))?");

Next, a regular expression to parse verbose garbage collection logs from JRockit GC log files:

s/^\[memory \]\[\([^]]\+\)\].*GC
\([0-9]\+\)K->\([0-9]\+\)K (\([0-9]\+\)K), \([0-9]\+\.[0-9]\+\) ms/\1,\2,\3,\4,\5/

This will return various columns including memory before and after collection, max heap size, and collection duration.

Similarly, for Sun JVMs:

s/.*GC \([0-9]\+\)K->\([0-9]\+\)K(\([0-9]\+\)K), \([0-9]\+\.[0-9]\+\) secs]/\1,\2,\3,\4/

Verbose GC logging is usually enabled via the -XXloggc option, e.g. -Xloggc:gc.log