Skip to main content

Read matches and capture groups

A full match is the text consumed by the whole pattern. A capture group stores part of that match for later use.

Free
Included
Pro
Included
Self-hosted
Included
Compare plans

Capture an order identifier​

Use JavaScript with g and this pattern:

\bORD-(?<id>[0-9]{4})\b
ORD-0042 pending
ORD-0187 sent
ORD-42 invalid
XORD-0001 invalid

Open in regex101

MatchGroup 0 (full match)Group 1, named id
FirstORD-00420042
SecondORD-01870187

The name id describes the captured value. Group numbers follow the opening capturing parentheses. A noncapturing group, such as (?:pending|sent), groups alternatives without creating a captured value.

Read the explanation​

  1. Read the Explanation panel for the pattern structure.
  2. Locate ORD-0042 in the highlighted input.
  3. Inspect its group id in Match Information. The captured value is 0042.

For this example, \b requires a word boundary. [0-9] selects an ASCII digit, and {4} repeats it four times. The named group surrounds those four digits.

The Quick Reference provides syntax reminders for the selected flavor. Choose a category from the menu above the tokens. Select a token to read its explanation.

To search all categories, select Search reference in the panel header. Press Escape in the search field to return to your category.

Distinguish an empty group from an absent group​

An optional group can be absent from a successful match. A participating group can also capture an empty string. These states differ in unit-test expectations, and engines can represent them differently.

Capture group syntax also differs between engines. Python uses (?P<id>...) for this named group. Changing a flavor can require a pattern change.

The two full matches contain IDs 0042 and 0187.
Named group id links the highlighted digits to the match details.