liveBook · Manning

POSIX character class

As explained in The Well-Grounded Rubyist, Fourth Edition

POSIX bracket notation is a standardized regular-expression syntax for specifying character classes using bracket expressions.

A POSIX character class uses bracket notation inside a Ruby regular expression to match one character from a named class. Forms such as [[:alpha:]], [[:word:]], and [[:digit:]] provide alternatives to explicit bracket lists and can recognize Unicode characters that Ruby's shorthand classes may not. section 11.3.3 section 11.8

Unicode coverage

Ruby's /\w/ does not match the character Ē, but /[[:word:]]/ does. This makes the POSIX form useful when a word-like class must include the relevant Unicode characters. section 11.3.3

>> "\u0112".match(/\w/)
=> nil
>> "\u0112".match(/[[:word:]]/)
=> #<MatchData "Ē">

Compare Ruby's \w shortcut with the POSIX word class in an irb-sized example. section 11.3.3

Available names include :alpha:, :upper:, :lower:, :word:, and :digit:. POSIX classes also have Unicode-property equivalents, such as \p{Alpha} for [[:alpha]]. Detailed property behavior is left for further investigation. section 11.3.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines bracket syntax, contrasts it with Ruby shortcuts, and notes property equivalents while deferring further Unicode detail.
Examples
Uses [[:word:]] and \w to contrast matching the Unicode character Ē.
What it assumes
Assumes familiarity with Ruby regular expressions, bracket expressions, and shorthand classes such as \w and \d.
What you can do after
Apply it via Ruby regular expressions to match named character classes with broader Unicode coverage than some shortcuts.
How it connects
Connects character classes with regular expressions, Unicode properties, and match operations.

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.