MEPX
Chapter 5 of 10All chapters

Chapter 5 of 10

Groups and alternation

Structure inside a pattern.

Capturing

Parentheses group parts together and capture what they matched, numbered from left to right. Captures are how you extract fields rather than just testing for a match.

  • Non-capturing groups start with ?: and keep numbering meaningful.
  • Named groups make replacement strings readable months later.

Alternation

A vertical bar means or, and it applies to the whole pattern unless grouped. Wrapping alternatives in a group is nearly always what you meant.