liveBook · Manning

Regular expression atom

As explained in The Well-Grounded Rubyist, Fourth Edition

A regular expression atom is a single repeatable unit of a regular-expression pattern, such as a character, character class, or parenthesized subpattern.

A regular expression atom is one repeatable part of a pattern: an individual character, a character class, or a parenthesized subpattern. Quantifiers can repeat any of these atoms, not only single characters. section 11.5.2

Repeating a character class

Use /\d{3}-\d{4}/ for a fixed-width pattern, /\d{1,10}/ for one to ten digits, and /\d{3,}/ for three or more. Ruby rejects impossible ranges such as {10,2}. section 11.5.2

Repeat versus capture

In /([A-Z]){5}/, the whole match can be BLACK, but the first capture is only K: the parenthesized atom matches one character repeatedly and retains the last capture. Move the quantifier inside the parentheses—/([A-Z]{5})/—to capture BLACK.

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains atom types, repetition ranges, impossible bounds, and repeated-capture behavior before deferring conditional matching.
Examples
Demonstrates digit ranges, five uppercase letters, and the contrast between repeated and grouped captures.
What it assumes
Assumes familiarity with regular-expression syntax while defining atoms through characters, character classes, and parenthesized subpatterns.
What you can do after
Diagnose why a repeated parenthesized atom captures only its final iteration and restructure the pattern to capture the whole match.
How it connects
Connects atoms with quantifiers, character classes, subpatterns, captures, and MatchData.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.