Regular expression train track diagrams
Mar. 17th, 2021 11:35 pmI'd only ever seen train track diagrams for SQL. They're a good way of showing how the syntax works rather than describing it. Turns out you can do that with regex too.
I wrote a regex to parse garbage collection logs:
And here's a regex that matches numbers divisible by 3:
First, it seems that you can match numbers divisible by anything (!) in regex. And second, I'm not sure it helps that much to make a divisible-by-3 train track diagram. I'm thinking "why does that work?" and at the same time "Please don't explain".
I wrote a regex to parse garbage collection logs:
(?:[-0-9.T:+]+: )?([0-9.]+): \[((?:Full )?GC(?:--|AdaptiveSizePolicy::compute_survivor_space_size_and_thresh:)? ?(?:\((?:System|Ergonomics)\)(?:AdaptiveSizeStart:)? )?(?:[0-9.]+: )?(?:[0-9.]+: )?).*?(?:\[(?:DefNew(?: \(promotion failed\) )?|PSYoungGen): ([0-9]+)K->([0-9]+)K\(([0-9]+)K\)(?:, [0-9.]+ secs)?\])?(?:[0-9.]+:)?(?: )?(?:\[(?:Tenured|PSOldGen|ParOldGen): ([0-9]+)K->([0-9]+)K\(([0-9]+)K\)(?:, [0-9.]+ secs)?\])? ?([0-9]+)K->([0-9]+)K\(([0-9]+)K\),? (?:\[(?:Perm|PSPermGen|Metaspace) ?: ([0-9]+)K->([0-9]+)K\(([0-9]+)K\)\], )?([0-9.]+) secs\](?:.*?)?Good luck working out what that does, unless you turn it into train tracks.
And here's a regex that matches numbers divisible by 3:
([0369]|[147][0369]*[258]|(([258]|[147][0369]*[147])([0369]|[258][0369]*[147])*([147]|[258][0369]*[258])))+source
First, it seems that you can match numbers divisible by anything (!) in regex. And second, I'm not sure it helps that much to make a divisible-by-3 train track diagram. I'm thinking "why does that work?" and at the same time "Please don't explain".