MEPX
Chapter 10 of 10All chapters

Chapter 10 of 10

Writing readable patterns

For the person reading it later.

Habits

Build the pattern in pieces and test each one. Use extended mode with whitespace and comments where the language supports it, and name your capture groups.

  • A regex tester with an explanation pane teaches faster than documentation.
  • Always test the cases that should not match, not only the ones that should.

Knowing when to stop

If a pattern needs a paragraph of explanation, a small parser or a few lines of ordinary code will be clearer and easier to change. Regex is a tool, not a prize.