Guide to Regular Expressions in Ruby
In this section of the course, we are going to talk about some regular expressions in Ruby. The goal of this lesson is to familiarize you with expressions in Ruby, and to give you the tools necessary to expand your knowledge. As always, we will learn with a practical example that you can use while doing real-time programming.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this section of the course, we are going to talk about using regular expressions in Ruby. The goal of this guide is to familiarize you with regular expressions in Ruby and to give you the tools necessary to expand your knowledge. As always, we will learn with a practical example that you can use while doing real world programming.

Ruby Regular Expression Code Example

So, let's start with some basic regular expression matchers. We'll begin with storing a string sentence in a variable:

string = "The quick 12 brown foxes jumped over 10 lazy dogs"

First off, let's check if the above string contains the letter o and to do that write the following code:

p string =~ /o/

devCamp Note: In Ruby anything contained within the // is the regular expression.

Now, if you execute this code, the output should be: 15. The value printed out is 15 because the matcher found the letter o is in position 15.

If you want to find an entire word, then you can do that with the code:

p string =~ /quick/

The output is 4 because the word quick begins at position 4.

A more practical example would be to check if a letter, let's say z is present in a string. If it's present, then it's a valid string, otherwise it's invalid. To check this, we'll use the code:

p string =~ /z/ ? "Valid" : "Invalid"

I'm using the ternary operator for this code. The ternary mechanism allows us to place an if/else condition on the same line. Our ternary code above is exactly the same as doing:

if string =~ /z/
  p "Valid"
else
  p "Invalid"
end

The ternary operator can come in handy because, as you can see, we were able to take five lines of code and combine it into a single line, resulting in the same functionality.

If you execute this program, your output is Valid.

Now, let's change it a little bit to replace z with Z.

p string =~ /Z/ ? "Valid" : "Invalid"

The output now will be Invalid because this expression is case-sensitive. To make it case insensitive, you have to add i to the expression like this:

p string =~ /Z/i ? "Valid" : "Invalid"

Now, if you execute the code, the output is Valid.

The last thing we are going to try is to return all the integer values from the sentence.

p string.to_enum(:scan, /\d+/).map {Regexp.last_match}

In this code, we are first converting the string to an enumerator and passing the method scan to it. This method is a part of Ruby's regular expression's library, and along with it, we are passing a regular expression. The \d is the expression that searches for integers, while the + looks for multiple instances. If I don't have the + sign, then it will pick only the first integer that it finds. Regexp is the class that Ruby has for regular expressions and last_match is one of the methods available in this class.

If you execute this code, the output will be:

[#<MatchData "12">, #<MatchData "10">]

This is perfect because we have only two integers: 10 and 12, in the string, which is what the code printed out.

So, with just one line of code, we were able to scan the string, pick integers and display their value.