character_classes
🔢 Use Character Classes and Shorthand​
Character classes let you match sets of characters concisely. You can combine shorthand classes like \d
(digits) and custom ranges like [A-Fa-f]
to fine-tune your patterns.
pattern = /[A-Za-z]\w{2,10}\d*/ # starts with a letter, 2–10 word chars, optional digits
'Hello123'.match?(pattern) # => true
'1Hello'.match?(pattern) # => false
Use \s
, \S
, \w
, \W
, \d
, and \D
to cover common classes without lengthy lists.