Regex Explainer

What each piece of a pattern actually does.

What each piece of a pattern actually does.

How to use it

  1. Paste the pattern. Without the slashes. It is compiled first, so an invalid one is refused.
  2. Read the parts. In order, with a sentence for each construct.
  3. Set the flags. They are listed too, since a flag changes what several parts mean.

When you would use this

Most regular expressions are read far more often than they are written, usually by someone who did not write them and now has to change one. This breaks a pattern into its constructs and gives each a plain sentence. The ones people look up repeatedly are all covered: what a non capturing group is for, why \b matches a position rather than a character, the difference between a greedy and a lazy quantifier, and which of the four lookaround forms is which. Runs of ordinary characters are grouped into one row, so a word reads as a word rather than as one row per letter. It names the pieces rather than describing the structure. A structural explanation needs a full parser and a way to draw the result, and the piece level is where the confusion usually is. Flags are listed alongside, because a flag changes what other parts mean: multiline changes what ^ and $ match, and dotall changes what a full stop matches, so a parts list without them would be misleading.

Questions

Does it explain the structure or just the pieces?
The pieces, in order. Explaining structure needs a full parser and a way to draw it, and most of the value is in naming the construct someone is staring at: what (?: does, why \b matches nothing, the difference between +? and +.
Why does it refuse an invalid pattern?
Because explaining something that will not compile would describe a pattern that does not exist. It is compiled first, so you get the compiler's reason instead, which is more useful.
Why are the flags listed separately?
Because a flag changes the meaning of parts elsewhere in the pattern. Multiline changes what ^ and $ match, and dotall changes what a full stop matches, so the parts list would be wrong without them stated.