O'Reilly logo

Text Processing with Ruby by Rob Miller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required