String#scan
str.scan(pattern) -> array Returns:
array · Updated March 14, 2026 · String Methods strings regex pattern-matching regular-expressions
The scan method searches a string for all occurrences of a given pattern and returns an array containing each match. Unlike match which returns only the first match, scan finds every occurrence.
Syntax
string.scan(pattern)
When given a regex pattern, scan returns an array of all matching substrings. If the regex contains capturing groups, each element of the returned array is an array of the captured groups.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | Regexp or String | — | The pattern to search for. Can be a regular expression or a plain string. |
Examples
Basic string matching
text = "the quick brown fox jumps over the lazy dog"
text.scan("the")
# => ["the", "the"]
Using regular expressions
text = "I have 3 apples and 5 oranges"
text.scan(/\d+/)
# => ["3", "5"]
text.scan(/[aeiou]/)
# => ["a", "e", "a", "o", "e", "a", "o", "u", "e"]
With capturing groups
When the pattern contains capturing groups, scan returns arrays of the captured groups:
text = "John:25, Jane:30, Bob:22"
text.scan(/(\w+):(\d+)/)
# => [["John", "25"], ["Jane", "30"], ["Bob", "22"]]
Extracting emails from text
content = "Contact us at support@example.com or sales@company.org for help"
content.scan(/[\w.-]+@[\w.-]+\.\w+/)
# => ["support@example.com", "sales@company.org"]
Finding all words of a certain length
sentence = "Ruby is a powerful and expressive programming language"
sentence.scan(/\b\w{4,}\b/)
# => ["Ruby", "powerful", "expressive", "programming", "language"]
Common Patterns
With block form
When given a block, scan yields each match and returns the original string:
"hello world".scan(/\w+/) { |word| puts word.upcase }
# HELLO
# WORLD
# => "hello world"
Building a word frequency counter
text = "the quick brown fox jumps over the lazy dog the fox is quick"
words = text.scan(/\b\w+\b/)
frequency = words.tally
# => {"the"=>2, "quick"=>2, "brown"=>1, "fox"=>2, "jumps"=>1, ...}
Parsing structured data
data = "ID:001,ID:002,ID:003"
ids = data.scan(/ID:(\d+)/).flatten
# => ["001", "002", "003"]