Ruby:
Using regular expressions
How to:
Basic Matching
To match a string against a simple pattern, you can use the match method. Below, we’re checking if the word “Ruby” exists in a given string.
if /Ruby/.match("Hello, Ruby!")
puts "Match found!"
end
# Output: Match found!Pattern Matching with Variables
You can interpolate variables into your regex using the #{} syntax, making your patterns dynamic.
language = "Ruby"
if /#{language}/.match("Programming in Ruby is fun.")
puts "Talking about Ruby!"
end
# Output: Talking about Ruby!Using Regex for Substitution
The gsub method allows you to replace every occurrence of a pattern with a specified replacement string.
puts "foobarfoo".gsub(/foo/, "bar")
# Output: barbarbarCapturing
Parentheses in a regex are used for capturing parts of a match. The match method returns a MatchData object, which you can use to access captures.
match_data = /(\w+): (\d+)/.match("Age: 30")
puts match_data[1] # Captured label
puts match_data[2] # Captured value
# Output:
# Age
# 30Using Third-Party Libraries
Although Ruby’s standard library is powerful, you might sometimes need more specialized functionality. One popular gem for working with regex is Oniguruma, which provides additional regex features beyond the built-in Ruby regex engine.
Install it using:
gem install onigurumaExample usage could look like this (assuming you have required oniguruma after installing it):
# This is a more advanced example and might require additional setup
require 'oniguruma'
pattern = Oniguruma::ORegexp.new('(\d+)')
match_data = pattern.match("The number is 42.")
puts match_data[1]
# Output: 42Remember, while powerful, regular expressions can become complex and hard to manage for more complicated patterns. Aim for readability, and consider alternative methods if your regex becomes too convoluted.