Performing Matches
Although the brunt of this chapter is on the syntax of regular expressions, and specifically the Perl-Compatible Regular Expressions (PCRE) that Ruby implements, you’ll need to know what operators and methods make use of regular expressions. To start, there is the pattern matching operator, =~
. This operator is used with one String
and one Regexp
(the Ruby class that represents regular expressions). Logically, you might use this operator in a condition:
puts 'match' if str =~ regex
It doesn’t matter in what order you place the string and the regular expression:
puts 'match' if regex =~ str
Get Ruby: Visual Quickstart Guide 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.