Regular Expressions in Ruby

In Ruby, regular expressions are a first-class construct. Literal regexes have their own syntax (//), and regular expressions are taken as arguments by countless methods in the standard library. This is particularly true of the String class, but regular expressions are also used in other, perhaps less obvious, places—Enumerable#grep, for example. They’re definitely not an afterthought; they are as fundamental to the language as strings or numbers.

Let’s take a look at how we can define regular expressions in Ruby so that we can use our newfound knowledge.

Defining Regular Expressions

Regular expressions in Ruby are instances of the Regexp class. Technically, you can create one by passing a string to Regexp.new, but ...

Get Text Processing with Ruby now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.