Regex usage in Ruby

Regular expressions, or regex for short, are a powerful tool used for pattern matching and text processing. Ruby has a built-in regular expression engine that provides a simple and efficient way to work with regex. In this article, we'll explore how to use it.

Creating Regular Expressions in Ruby

A regular expression is a pattern that specifies a set of matching strings. In Ruby, a regex is represented by a special syntax that is enclosed in forward slashes (/). For example, to create a regex pattern that matches any string that contains the word "ruby", we can use the following code:

      
  /ruby/
      
    

This regex will match any string that contains the characters "ruby" in that order. We can also use regex metacharacters to specify more complex patterns. For example, to match any string that starts with "ruby" and ends with a digit, we can use the following pattern:

      
  /^ruby\d$/

    

This regex uses the ^ and $ metacharacters to match the start and end of the string, respectively. The \d metacharacter matches any digit.

Using Regular Expressions as a condition ?

      
  if "Do you like cats?".match(/like/)
    puts "Match found!"
  end
      
    

Using Regular Expressions as to check if string contains a number ?

      
  def contains_number(str)
    str =~ /[0-9]/
  end
  contains_number("The year is 2015")
      
    

Once we have created a regex pattern, we can use it in our Ruby code to perform pattern matching and text processing. The most common method for using regex in Ruby is the match method, which is available on string objects. The match method returns a MatchData object that contains information about the matched string. Here's an example:

      
  string = "ruby123"
  match_data = string.match(/ruby\d/)
  puts match_data[0]
      
    

Ways to apply regular expression to string:

In addition to the match method, Ruby provides a number of other methods for applying regex to strings. Here are some of the most commonly used methods:

Here's an example that demonstrates how to use the sub method to replace a word in a string:

      
  string = "The quick brown fox jumps over the lazy dog."
  regex = /brown/
  new_string = string.sub(regex, "red")
  puts new_string
      
    

In this code, we create a string string, a regex pattern regex, and a replacement string "red". We then call the sub method on the string with the regex pattern and replacement string as arguments. The sub method replaces the first occurrence of the word "brown" in the string with the word "red".

Regex Capture Groups

With capture groups, we can capture part of a match and reuse it later. T o capture a match we enclose the part we want to capture in parenthesis!

      
  Line = Struct.new(:time, :type, :msg)
  R_FORMAT = /(\d{2}:\d{2}) (\w+) (.*)/
  def parse_line(line)
    line.match(R_FORMAT) { |m| Line.new(*m.captures) }
  end
  parse_line("12:41 INFO User has logged in.")

# !Will produce 
      
    

Conclusion

Regex is a powerful tool for pattern matching and text processing, and Ruby provides a simple and efficient way to work with regex patterns.