C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Success: If the expression does match, though, we can find more out about the matched data.
Here: We iterate with "each" over 3 strings in a string array. We see whether the string matches the pattern.
And: If the match method returns a value, the operation succeeded. Otherwise, "m" is nil.
Ruby program that uses match
values = ["123", "abc", "456"]
# Iterate over each string.
values.each do |v|
# Try to match this pattern.
m = /\d\d\d/.match(v)
# If match is not nil, display it.
if m
puts m
end
end
Output
123
456
Pattern details
\d A digit character 0-9.
\d\d\d Three digit characters.
Return: If the operator returns nil, the match failed. But if the matching was successful, an integer is returned.
Tip: This integer is the index at which the match occurred. We often can use this value in an if-statement to indicate success.
Ruby program that uses match operator
# The string input.
input = "plutarch"
# If string matches this pattern, display something.
if /p.*/ =~ input
puts "lives"
end
Output
lives
Pattern details
p Matches lowercase letter p.
.* Matches zero or more characters of any type.
Here: Two strings both have a common pattern: a space is followed by a letter "A."
And: With the "i" flag specified on the regular expression in the match expression, both strings are matched, despite the case difference.
Ruby program that ignores case, Regexp
# A string array.
names = ["Marcus Aurelius", "sam allen"]
# Test each name.
names.each do |name|
# Use case-insensitive regular expression.
if /\ a/i =~ name
puts name
end
end
Output
Marcus Aurelius
sam allen
Pattern description
"\ " Matches a space.
"a" Matches the letter "a".
i Specifies the expression is case-insensitive.
Tip: Patterns are used throughout Ruby string methods. With gsub we use a string method with a regular expression.
Ruby program that uses gsub
value = "caaat"
# Replace multiple "a" letters with one.
result = value.gsub(/a+/, "a")
puts value
puts result
Output
caaat
cat