Ruby's Regexp Class
A regular expression (regex) is a sequence of characters that define how to search and match patterns in a string. It is commonly used in all modern programming languages like Java, Ruby, and Python.
Ruby uses the class Regexp to store a regex. Regexp are delimited using the following literals and constructor:
/.../
%r{...}
Regexp::new
You can call the match() method to check whether a string contains the pattern. For instance:
/I love apples/.match("love")
will return either nil if a match is not found, or a MatchData object if it is found. There is another similar operator =~ that returns the index of the first match in the string, otherwise it returns nil.
The match method also accepts a block, which will be executed with MatchData if match succeeded. See below:
sentence.match(pattern) do |match|
...
end
You can also use [] which is like match:
"foo+account@gmail.com"\[/\\+(\[^@\]+)/, 1\]
This will match capture group 1 which, in the above example, is what is inside ().
You can use ?<name> to name capture groups so that you can reference them when performing match:
/\\$(?<dollars>\\d+)\\.(?<cents>\\d+)/.match("$3.67")\[:dollars\]
The above will return “3”.
Regexp also has a static class method last_match() that returns the MatchData object that is generated by the previous successful pattern match in your code. This is equivalent to reading the global variable $~.MatchData. So for example:
/I love apples/.match("love")
puts Regexp.last\_match(0)
Here, it will print out the first match in the expression.