Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Ruby Dictionary

  1. Home
  2. Ruby Dictionary
  3. String.scan / match / =~

String.scan / match / =~

Methods for searching strings using regular expressions. You can retrieve all matches as an array, or get detailed information via a MatchData object.

Syntax

# Returns all substrings matching the pattern as an array.
string.scan(/pattern/)
string.scan(/pattern/) { |match| process }

# Returns the MatchData for the first match (nil if no match).
string.match(/pattern/)
string.match(/pattern/, start_pos)

# Returns the position of the match (same as =~).
string =~ /pattern/

Method List

MethodDescription
scan(pattern)Returns all substrings matching the pattern as an array. If capture groups are present, returns an array of arrays.
scan(pattern) { |m| }Calls the block for each match.
match(pattern)Returns the MatchData for the first match. Returns nil if there is no match.
match(pattern, pos)Starts the search from the character at position pos.
=~Returns the position (integer) of the first match, or nil if there is no match.

Sample Code

text = "Ruby was born in 1995, and version 3.2 was released in 2022."

# Use scan to extract all numbers.
digits = text.scan(/\d+/)
puts digits.inspect  # ["1995", "3", "2", "2022"]

# Use capture groups to extract the year, month, and day separately.
date_str = "Scheduled: 2024-01-15, 2024-03-20, 2024-06-01"
date_list = date_str.scan(/(\d{4})-(\d{2})-(\d{2})/)
puts date_list.inspect
# [["2024", "01", "15"], ["2024", "03", "20"], ["2024", "06", "01"]]

# Process each match using a block.
puts "Matched dates:"
date_str.scan(/\d{4}-\d{2}-\d{2}/) { |date| puts "  #{date}" }

# Use match to get detailed information about the first match.
html = "<h1>タイトル</h1>"
m = html.match(/<(\w+)>(.+?)<\/\1>/)
if m
  puts "Tag: #{m[1]}"      # Tag: h1
  puts "Content: #{m[2]}"  # Content: タイトル
end

# Use match with a starting position.
sentence = "apple banana apple cherry"
m1 = sentence.match(/apple/)
puts m1[0]  # apple (first match)
m2 = sentence.match(/apple/, 6)
puts m2[0]  # apple (search starts from character 6)
puts m2.begin(0)  # 14 (position of the second "apple")

Notes

Use scan when you want to retrieve all matches at once. Without capture groups it returns an array of strings; with capture groups it returns an array of arrays. This makes it useful for data extraction.

Use match when you only need the first match, or when you need detailed information such as the match position or the surrounding text. The returned MatchData object provides methods such as begin(n) (start position of the match), end(n) (end position), pre_match, and post_match.

Using regular expressions to parse HTML or XML is not recommended. There are many cases — such as nested structures and attribute ordering — that regular expressions cannot handle correctly. Use a dedicated library such as Nokogiri to parse HTML.

For the basic syntax of regular expressions, see Regexp.new / =~ / match. For string replacement, see String#sub / gsub.

If you find any errors or copyright issues, please .