简体   繁体   中英

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.

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

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"

In this case, you should use a pattern matching instead of split .

line = "25 red delicious apples at 0.75\n"
line.match(/(\d+)\s+(.*)\s+at\s+(\S+)/).values_at(1, 2, 3)
# => ["25", "red delicious apples", "0.75"]
p "25 red delicious apples 0.75".partition(/[\D\s]+/)
#=> ["25", " red delicious apples ", "0.75"]
'25 red delicious apples at 0.75'.scan(/[0-9]+\.?\d*/) #=> ["25", "0.75"]

How about:

'25 red delicious apples at 0.75'.scan /(\d+[.\d]+) (.*) at (\d+[.\d]+)/
#=> [["25", "red delicious apples", "0.75"]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM