Q:

Regex in Ruby for multiple splits in a string

I need some advice perfecting a regex. I'm trying to split a string into three pieces with a single expression. Lines come from a text file in a format like so: 
25 red delicious apples at 0.75 

where the first part is the quantity, the second is the item name, and the third is the price per item. The code I'm using is this: 
File.open('basket.txt').each_line do |line|
  item = line.split(/(\d+)\s|\sat\s/, 3)

This splits a string where I want it, but it creates an item array with length four (the first index contains nil). I also want to get rid of the newline character at the end of the float.

A:

You can try this:
txt = "25 red delicious apples 0.75"
pattern = Regexp.new('(?<=\d)\s|\s(?=\d)')
puts txt.split(pattern)

or with irb:
'25 red delicious apples 0.75'.split(/(?<=\d)\s|\s(?=\d)/)

with "at":
'25 red delicious apples at 0.75'.split(/(?<=\d)\s|\sat\s(?=\d)/)

An example with your loop:
pattern = Regexp.new('(?<=\d)\s|\sat\s(?=\d)')
File.open('basket.txt').each_line do |line|
  items = line.split(pattern)
end

A:

I would use match instead of split for this task. This way you will be able to get the groups more accurately. For instance if we assume there are no numbers in the name of the product:
s = "25 red delicious apples 0.75"
m = s.match(/(\d+) ([^\d.]+) ([\d.]+)/)
m[1]
=> "25"
m[2]
=> "red delicious apples"
m[3]
=> "0.75"

