[Ruby] Difference between match / scan

I will actually write

# irb
a = "barfoobazfoobaofoo"

#For match
a.match(/ba./)
=> #<MatchData "bar">

#For scan
a.scan(/ba./)
=> ["bar", "baz", "bao"]

match returns a MatchData object scan is returned as an array.

If the regular expression doesn't match

#irb

# match
a.match(/baa./)
=> nil

# scan
a.scan(/baa./)
=> []

For match, nil is returned, scan returns an empty array.

Consideration

Consideration 1

match does not return an array, so trying to loop through the results will result in an error. Since scan returns an array, it can be looped. (In other words, scan can be processed by passing the block as it is, so you can do this ↓)

# irb
a.scan(/ba./) {|s| p s*2}
"barbar"
"bazbaz"
"baobao"

Consideration 2

Since match returns nil, it becomes false at the time of conditional branching. Note that scan is [], so it will be true in conditional branching.

reference

https://docs.ruby-lang.org/ja/latest/method/String/i/match.html https://docs.ruby-lang.org/ja/latest/method/String/i/scan.html

Recommended Posts

[Ruby] Difference between match / scan
[Ruby] Difference between get and post
[Ruby] Difference between is_a? And instance_of?
Note: Difference between Ruby "p" and "puts"
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Ruby] Difference between print, puts and p
[Ruby] Difference between puts and return, output and return value
Difference between vh and%
Difference between i ++ and ++ i
[Ruby] I thought about the difference between each_with_index and each.with_index
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
About the difference between classes and instances in Ruby
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between CUI and GUI
About the difference between "(double quotation)" and "single quotation" in Ruby
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between bundle and bundle install
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
The difference between programming with Ruby classes and programming without it
Difference between render and redirect_to
[Ruby] Maybe you don't really understand? [Difference between class and module]
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Easy to understand the difference between Ruby instance method and class method.
Difference between instance method and class method
Difference between render method and redirect_to
Find the difference between List types
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;