liveBook · Manning

Dot

As explained in The Well-Grounded Rubyist, Fourth Edition

The dot wildcard character is the regular-expression metacharacter that normally matches any character except a newline.

In a regular expression, the dot (.) is a metacharacter that matches one character, except a newline. The pattern /.ejected/ therefore matches “dejected,” “rejected,” “%ejected,” and “8ejected.” section 11.3.2 section 11.3

/.ejected/

Test the wildcard against a string whose first character varies. section 11.3.2

The wildcard can admit more matches than intended. Use a character class when the variable position must be restricted to a known set of characters. By default, a newline stops a dot match; the m modifier makes dots match newline characters too. section 11.3.2 section 11.5.4

Matching text across lines

The pattern /\(.*?\)/m captures parenthesized text even when it contains embedded newlines. Without m, the dot reaches the first newline and the match fails when the closing parenthesis is not on that line. section 11.5.4

A dot is special inside a regexp but literal inside an ordinary string. Interpolating that string into a regexp gives the dot wildcard meaning, so Regexp.escape is needed when the interpolated text must match exactly. section 11.3.1 section 11.6.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines matching behavior, contrasts character classes and modifiers, and explains interpolation escaping without descending into regexp engine internals.
Examples
Demonstrates /.ejected/, multiline parenthesized matching, and interpolated strings containing literal periods.
What it assumes
Assumes basic regexp literals and string matching while teaching metacharacters, character classes, modifiers, and regexp escaping.
What you can do after
Apply the dot in Ruby regular expressions, choose multiline matching deliberately, and escape interpolated text when literal matching is required.
How it connects
Connects the dot with regular expressions, character classes, regexp modifiers, interpolation, and escaping.

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.