Regexp.new / =~ / match / match?
The basic syntax for pattern matching with regular expressions. You can check whether a string matches a specific pattern, and retrieve information about the match.
Syntax
# Regular expression literal (most common).
/pattern/
/pattern/i # Case-insensitive matching.
/pattern/m # Makes . match newlines as well.
# Dynamically create a regular expression using the Regexp class.
Regexp.new("pattern")
Regexp.new("pattern", Regexp::IGNORECASE)
# =~ operator: returns the position of the match (nil if no match).
string =~ /pattern/
/pattern/ =~ string
# match method: returns a MatchData object.
string.match(/pattern/)
/pattern/.match(string)
# match? method: returns a boolean indicating whether a match exists (Ruby 2.4+).
string.match?(/pattern/)
# !~ operator: returns true if the string does not match.
string !~ /pattern/
Method List
| Method / Operator | Description |
|---|---|
| Regexp.new(str) | Dynamically creates a regular expression object from a string. |
| =~ | Returns the index (0-based) of the first match. Returns nil if there is no match. |
| match(pattern) | Returns a MatchData object. Useful for accessing capture groups. |
| match?(pattern) | Returns true or false indicating whether a match exists. Faster than match because it does not create a MatchData object. |
| !~ | Returns true if the string does not match. |
Sample Code
str = "Hello, Ruby 3.2!"
# Get the match position using =~.
pos = str =~ /Ruby/
puts pos # 7 (index where "Ruby" starts)
puts (str =~ /Python/).inspect # nil (no match)
# Get MatchData using match.
md = str.match(/(\w+)\s+(\d+\.\d+)/)
puts md[0] # Ruby 3.2 (entire match)
puts md[1] # Ruby (first capture group)
puts md[2] # 3.2 (second capture group)
# Named capture groups.
date_str = "2024-03-15"
m = date_str.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/)
puts m[:year] # 2024
puts m[:month] # 03
puts m[:day] # 15
# Simple check using match? (faster because no MatchData is created).
puts "test@example.com".match?(/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i) # true
puts "not-an-email".match?(/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i) # false
# Dynamically create a regular expression using Regexp.new.
keyword = "Ruby"
pattern = Regexp.new(keyword, Regexp::IGNORECASE)
puts "I love ruby programming".match?(pattern) # true
Notes
Regular expressions are deeply integrated into Ruby, and the literal syntax (/pattern/) keeps code concise. The =~ operator returns the match position as an integer, or nil if there is no match — both can be used directly in conditionals, since nil is falsy and an integer is truthy.
The MatchData object returned by match lets you access the full match ([0]), numbered capture groups ([1], [2], ...), and named captures ([:name]). Use pre_match and post_match to retrieve the text before and after the match.
Passing user input directly to Regexp.new can lead to unintended patterns or a ReDoS (Regular Expression Denial of Service) vulnerability. Always use Regexp.escape when building regular expressions dynamically from external input.
To search a string for all matches using a regular expression, see string.scan / match. For string replacement, see string.sub / gsub.
If you find any errors or copyright issues, please contact us.