Intro to Regular Expressions in Ruby
Tags: regex, ruby, beginners, programming
Published: 2022-02-03 15:11:28

Hello, friends! Wherever you may be in your coding journey, if you haven't yet encountered regular expressions (what the cool kids refer to as regex), you will soon. 

You certainly have come across regex in your daily life- they are used to make sure the email address you entered is properly formatted and confirm that your password includes integers and special characters.

Let's start with a simple one. We will create a method that utilizes regex to see if a given word contains a vowel:
```ruby
def vowel (word)
    word =~ /[aeiou]/
end
```
First, the `=~` is an operator that checks a string against our regular expression. It returns the index of the string at which the first match occurs. If no match is found, the operator returns nil.

Second, since we want our method to determine if the given word contains **any** vowel, we place the vowels in question between brackets: `[aeiou]`. If we neglect the brackets, the regular expression would be looking to find a string with "aeiou" in that precise order. Hard to find one of those...

Finally, regular expressions in Ruby are written between two forward slashes to differentiate them from variables, strings, and arrays: `/[aeiou]/`.

Remember, the `=~` operator will return the first index of the match and nil if no match is found. Let's wrap our expression in a ternary and print a message to highlight what is happening under the hood. Let's also add some test cases to ensure our method works as intended.
```ruby
def vowel (word)
   puts word =~ /[aeiou]/ ? 
    "'#{word}' contains at least one vowel" : 
    "'#{word}' has no vowels"
end

vowel "hello"
vowel "world"
vowel "sky"
vowel "1"
vowel "I"
```
When we run this using `ruby [file_name].rb` in our terminal, we receive the following output:
```
'hello' contains at least one vowel
'world' contains at least one vowel
'sky' has no vowels
'1' has no vowels
'I' has no vowels
```
Whether "y" is or is not a value is beyond the scope of my grammatical expertise. But whether or not "I" is a vowel is certainly within the scope of our debugging expertise. Can you think of why "I" did not return a match for `[aeiou]`?

"I" did not return a match because our regex did not account for uppercase vowels. Let's first try "i" to test the validity of our hypothesis:
```ruby
vowel("i")
```
Which prints...
```
'i' contains at least one vowel
```
Beautiful. 

Time to update our method to account for uppercase vowels.

If we wanted to add that functionality to our regex, we could change the regular expression: `word =~ /[aeiouAEIOU]/`

We could also utilize the Ruby `#downcase` method to first convert the string to all lowercase letters, then check it against our regular expression:
```ruby
def vowel (word)
    downcased_word = word.downcase
    puts downcased_word =~ /[aeiou]/ ? 
    "'#{word}' contains at least one vowel" : 
    "'#{word}' has no vowels"
end
```
Now, let's execute our file with the "I" test case again:
```
'I' contains at least one vowel
```
Excellent.

To be sure, this is hardly scraping the surface of the power of regular expressions in ruby (and other languages). But if you understand the processes behind these examples, you are well on your way.

I recommend checking out [Rubular](https://rubular.com/), an online Ruby regex editor where you create regular expressions and input strings against which to test them. It also includes a quick reference guide at the bottom of the page to explain common regex syntax.

Happy coding.