Contents
Ruby Regex
Ruby Regular Expressions
A regular expression, or regex, is a sequence of characters that forms a search pattern. It’s mainly used for pattern matching within strings. In Ruby, regular expressions (regex) allow us to find specific patterns within a string. Two common uses of Ruby regex are validation (such as checking email addresses) and parsing text. Regular expressions in Ruby are enclosed between two forward slashes (/
).
Syntax:
# Finding the word 'hi'
"Hi there, I am using Ruby" =~ /hi/
This will return the index of the first occurrence of ‘hi’ in the string if found, otherwise, it will return nil
.
Checking if a String Contains a Pattern
You can check if a string contains a regex pattern using the match
method.
Example:
Match found
Checking for Specific Characters in a String
A character class lets you define a range of characters for matching. For instance, you can use [aeiou]
to search for any vowel.
Example:
# Ruby program using regular expressions
# Function to check if the string contains a vowel
def contains_vowel(str)
str =~ /[aeiou]/
end
# Driver code
puts contains_vowel("Hello") # 'Hello' has a vowel, so it returns index
puts contains_vowel("bcd") # 'bcd' has no vowels, returns nil, so nothing is printed
Output:
1
Common Regular Expressions
Here are some shorthand character classes for specifying ranges:
\w
is equivalent to[0-9a-zA-Z_]
\d
is the same as[0-9]
\s
matches any whitespace\W
matches anything not in[0-9a-zA-Z_]
\D
matches anything that’s not a number\S
matches anything that’s not a whitespace
The dot character .
matches any character except a newline. If you want to search for the literal .
character, you need to escape it with a backslash (\.
).
Example:
# Ruby program using regular expressions
str1 = "2m3"
str2 = "2.5"
# . matches any character
if str1.match(/\d.\d/)
puts "Match found"
else
puts "Not found"
end
# Escaping the dot to match only a literal '.'
if str1.match(/\d\.\d/)
puts "Match found"
else
puts "Not found"
end
# This will match because str2 contains a dot between digits
if str2.match(/\d\.\d/)
puts "Match found"
else
puts "Not found"
end
Output:
Match found
Not found
Match found
Explanation:
1. First condition: Checks if str1 contains a digit followed by any character and then another digit (\d.\d). Since str1 contains “2m3,” it matches.
2. Second condition: Escapes the dot (\.) to match a literal .. Since str1 doesn’t contain a literal dot, it does not match.
3. Third condition: Matches because str2 (“2.5”) contains a digit, followed by a literal dot, and then another digit.
In summary, Ruby regular expressions provide a powerful way to search and match patterns within strings, allowing for complex validations and parsing tasks.
Ruby Search and Replace
sub
and gsub
are Ruby string methods that utilize regular expressions for searching and replacing content in a string. Their in-place variants, sub!
and gsub!
, modify the original string directly.
sub
andsub!
: Replace only the first occurrence of the pattern.gsub
andgsub!
: Replace all occurrences of the pattern.
The difference between sub
/gsub
and their in-place variants (sub!
/gsub!
) is that the in-place methods (sub!
and gsub!
) modify the string on which they are called, while sub
and gsub
return a new string, leaving the original one unchanged.
Example:
# Ruby program demonstrating sub and gsub methods in a string
number = "1234-567-890 # This is a number"
# Remove the comment section using sub (replaces the first occurrence)
number = number.sub!(/#.*$/, "")
puts "Number: #{number}"
# Remove all non-digit characters using gsub (replaces all occurrences)
number = number.gsub!(/\D/, "")
puts "Number: #{number}"
Output:
Number: 1234-567-890
Number: 1234567890