Regexp Match Method
This page was last reviewed on Nov 16, 2022.
Dot Net Perls
Regexp. String processing is difficult. We must account for sequences and ranges of characters. Data is often imperfect—it has inconsistencies.
With Regexp, regular expressions, we use a text language to better handle this data. Ruby provides an operator, "~=" to make regular expressions easier to use.
First example. The match method applies a regular expression to a string parameter. If the regular expression does not fit, match returns nil.
Detail 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.
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
123 456
\d A digit character 0-9. \d\d\d Three digit characters.
Operator. This performs a task similar to the match method. We place the regular expression on the left side, then use the matching operator. A string goes on the right side.
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.
# The string input. input = "plutarch" # If string matches this pattern, display something. if /p.*/ =~ input puts "lives" end
p Matches lowercase letter p. .* Matches zero or more characters of any type.
Ignore case. A Regexp is by default case-sensitive. We can modify this by specifying a special flag "i," which stands for "ignore case" or case-insensitive.
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.
# 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
Marcus Aurelius sam allen
"\ " Matches a space. "a" Matches the letter "a". i Specifies the expression is case-insensitive.
Replace. With gsub we replace characters in a string based on a pattern. This is a replace() method that uses regular expressions.
Tip Patterns are used throughout Ruby string methods. With gsub we use a string method with a regular expression.
value = "caaat" # Replace multiple "a" letters with one. result = value.gsub(/a+/, "a") puts value puts result
caaat cat
Split. The split method also accepts regular expressions. We use a Regexp to specify the delimiter pattern. So we then extract the parts that are separated by the matching patterns.
String split
Word count. With split, we can count words in a string. We simply split on non-word characters and return the length of the array.
Word Count
A summary. Often we require no regular expressions. We just use string methods. But in many cases, complexities and inconsistencies surface. Regexp then becomes a better approach.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 16, 2022 (edit).
© 2007-2024 Sam Allen.