Find patterns in strings is a common problem for developers. One of the main scenarios where you could need this is on form validations, has the email the right structure? has the user first name any invalid character?. That's where regular expressions appear.

Let's Begin

In Ruby, regular expressions are defined between two slashes. Let me show you a simple example of regex in ruby:
def find_match(text)
  return true if text=~/hello/

  false
end

puts find_match("hello world") #true
puts find_match("nice to see you") #false
find_match
function will only check if the text contains hello

Match from at least one of the options

If you want to check if a text contains a character from a particular group of characters, you can use square brackets to accomplish this.
def match_range(text)
  return true if text=~/[0123456789]/

  false
end 

puts match_range("number 1") #true
puts match_range("number one") #false
While
/abc/
indicates "a and b and c",
/[abc]/
indicates "a or b or c". Instead of
/[0123456789]/
you can use
/[0-9]/
which generates the same result. Another common range is
/[a-z]/
to identify all the characters from the english alphabet.

Shorthand for ranges

Ruby provides a shorthand syntax for some of the ranges we frequently need. Let's see the most common: