b2KIT

Regex Railroad Diagram

Visualize regular expressions as railroad diagrams showing groups, quantifiers, and alternatives.

Tested tool guide Tested browser tools Checked August 16, 2026

What Regex Railroad Diagram does, with a checked example

A railroad diagram renders a regular expression as a track of boxes and forks read left to right: literals are labeled boxes, `|` alternatives are parallel tracks that split and rejoin, `?` adds a bypass, and `*` or `+` draw a loop back to the previous node. Each group becomes a labeled sub-diagram, so the pattern's nesting is visible at a glance. Paste a pattern and the tool lays out its structure. The common surprise: this is not a regex tester. It shows what the pattern says, not whether a string matches - and it exposes precedence, so `^a|b$` visibly splits into two anchored branches.

Worked example

A concrete input and expected output from the current implementation.

Input

(cat|dog)s?

Expected output

A diagram that opens group 1 and immediately forks: one track reads c-a-t and the other d-o-g, and the two rejoin at the group's end. After the group, the single track passes through a box labeled 's' while a straight bypass track skips it, both meeting before the end of the line.

The `|` inside the parentheses draws one track per alternative, and `?` draws a zero-or-one bypass around the 's'. Reading the tracks end to end yields the four accepted strings: cat, cats, dog, dogs.

How the result is produced

1

Precedence becomes layout

The diagram mirrors regex operator precedence, so it doubles as a precedence checker. Alternation is lowest: `a|b*` forks at the top into `a` and `b*`, with the `*` binding only to `b`. Quantifiers attach to the single preceding atom or group: `?` is a two-track fork with an empty branch, `*` and `+` are loops returning to the start of the previous box, and `{n,m}` loops with the count labeled. Groups render as numbered sub-diagrams.

2

Reading the tracks

Every path from the left end to the right end is one way the pattern can match: a box is text to consume, a fork is a choice of branches, a loop means repeat, and an empty branch means zero repetitions. Anchors such as `^` and `$` sit at the track's ends. Classes like `[a-z0-9]` and shorthands like `\d` render as single boxes, since the diagram shows structure, not the characters a class expands to.

Good uses

  • Debugging a pattern that matches strings you did not expect: the diagram exposes precedence traps, such as `^a|b$` splitting into two top-level branches `^a` and `b$` instead of anchoring both letters.
  • Documenting a pattern for humans: a diagram scans far faster than raw regex text, so it earns its place in code comments, README files, and pull request descriptions where the pattern itself would be opaque.
  • Checking what a quantifier repeats in a nested pattern: with `(ab)+c?`, the diagram makes it obvious that `+` repeats the whole group while `?` applies to `c` alone.

Limits and checks

  • It is a structure diagram, not a test run. It will not match strings, count occurrences, or show backtracking, and it gives no performance signal, so it cannot reveal a pattern that is correct but catastrophically slow to execute.
  • The rendering is flavor-neutral. Constructs such as lookarounds, backreferences, and lazy quantifiers exist in some regex engines but not others, so a pattern that draws cleanly can still be a syntax error in the engine you target. Confirm each construct against your engine's documentation.
  • Empty tracks are easy to miss. The bypass beside `?`, `*`, and `{0,n}` is the zero-repetition path, so `a*` can match nothing at all. A fork between two long branches can also hide that both alternatives share the same prefix.

Common questions

Can I paste a string and see whether it matches my pattern?

No. This tool visualizes structure; it does not evaluate input, so it will not tell you whether a given string matches. Use a regex tester or a short script in your target language for the match check, then come back to the diagram to understand why the pattern behaves that way.

Why does `a|bc` draw as two separate branches instead of one line?

Alternation has the lowest precedence of any regex operator, so `a|bc` means 'a' or 'bc': the pipe splits the whole expression, not just the `b`. To make the fork narrower, group it: `(a|b)c` draws the two branches inside a group followed by `c`, and matches 'ac' or 'bc'.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools