Skip to content

Alternation and order

Branches that never run

A pattern can be correct and still contain a line that can never fire.

If an earlier branch accepts everything a later one accepts, the later one is dead. [a-z0-9]+|[a-z]+ will never reach its second branch, because anything the second matches the first already did.

Dead branches are usually the ones somebody added to fix a bug. The bug was somewhere else, the branch went in anyway, and now the pattern is longer and does exactly what it did before.

Overlapping branches are worse than useless when a quantifier is wrapped around them. (a|a)+ has two ways to match every single character, so an input of twenty characters has a million ways to be matched — and the engine will try all of them before it admits the pattern does not fit.

That is the shape behind most catastrophic backtracking reports. Look at the step count on the second of these.

Matcher

Every claim on the left has a pattern under it. Press one and it runs here, against the same engine that grades a task.