Regexp

The Regexp class is the Ruby representation of a regular expression. We discussed the basic syntax of regular expressions at length in Chapter 8, Regular Expressions. Here we focus on the API for the Regexp class itself and then cover some advanced regular expression syntax.

You can create a regular expression using the literal syntax, which is two foreword slashes with the regular expression in the middle, such as /.*rb/. (All the escape sequences and whatnot inside the slashes are discussed in Chapter 8, Regular Expressions. The alternate delimiter %r{...} will also create regular expressions.

The method, Regexp.new(string, options = 0, timeout: nil) creates a new regular expression with the string argument as the pattern. The ...

Get Programming Ruby 3.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.