uses \d to match any digit, and the + that follows \d makes \d match as many digits in a row as possible. : scan « String « Ruby






uses \d to match any digit, and the + that follows \d makes \d match as many digits in a row as possible.


"The car costs $1000 and the cat costs $10".scan(/\d/) do |x|
  puts x
end

 








Related examples in the same category

1.extract numbers from a string
2.scan through all the vowels in a string: [aeiou] means "match any of a, e, i, o, or u."
3.specify ranges of characters inside the square brackets
4.Scan as split
5.Splitting Sentences into Words
6.scan a here document
7.Scan for \w+
8.Scan() string with hex value
9.scan(/./u) string with hex value
10.Count words for a string with quotation marks
11.Just like /\w+/, but doesn't consider underscore part of a word.
12.Anything that's not whitespace is a word.
13.Accept dashes and apostrophes as parts of words.
14.A pretty good heuristic for matching English words.