1

假设我有一个包含以下内容的字符串:

TODO    | Eat spaghetti.               | High   | food, happiness
TODO    | Get 8 hours of sleep.        | Low    | health
CURRENT | Party animal.                | Normal | socialization
CURRENT | Grok Ruby.                   | High   | development, ruby
DONE    | Have some tea.               | Normal |
TODO    | Destroy Facebook and Google. | High   | save humanity, conspiracy
TODO    | Hunt saber-toothed cats.     | Low    | wtf
DONE    | Do the 5th Ruby challenge.   | High   | ruby course, FMI, development, ruby
TODO    | Find missing socks.          | Low    |
CURRENT | Grow epic mustache.          | High   | sex appeal

阅读此类内容并将其存储在对象中的最佳方法是什么,请使用以下结构:

class example
  attr_accessor status
  attr_accessor description
  attr_accessor priority
  attr_accessor tags
end

我尝试使用以下正则表达式:

 /[a-zA-Z0-9]*/.match(text above)

但我得到的只是

#<MatchData "TODO">

我期望得到的是

[TODO, Eat spaghetti, High, food, happiness, TODO ... etc ]

实现这一目标的最佳方法是什么?

4

4 回答 4

1

您可能想使用 string.split 而不是 regex.match 请参阅文档

于 2013-11-03T12:15:38.993 回答
0
text.each_line.map{|l| l.strip.split(/\s*\|\s*/)}

结果:

[
  ["TODO", "Eat spaghetti.", "High", "food, happiness"],
  ["TODO", "Get 8 hours of sleep.", "Low", "health"],
  ["CURRENT", "Party animal.", "Normal", "socialization"],
  ["CURRENT", "Grok Ruby.", "High", "development, ruby"],
  ["DONE", "Have some tea.", "Normal"],
  [
    "TODO",
    "Destroy Facebook and Google.",
    "High",
    "save humanity, conspiracy"
  ],
  ["TODO", "Hunt saber-toothed cats.", "Low", "wtf"],
  [
    "DONE",
    "Do the 5th Ruby challenge.",
    "High",
    "ruby course, FMI, development, ruby"
  ],
  ["TODO", "Find missing socks.", "Low"],
  ["CURRENT", "Grow epic mustache.", "High", "sex appeal"]
]
于 2013-11-03T13:21:35.090 回答
0

为了将数据放入类中,我们需要 4 个步骤:

首先获取内存中的数据:

file = 'path/to/my/file.txt'
raw_data = File.read(file)

通过获取每一行来解析数据,将其拆分为 | 然后去掉空格和新行。

parsed_data = raw_data.lines.map{|line| line.split('|').map(&:strip)}
p parsed_data.first #=> ["TODO", "Eat spaghetti.", "High", "food, happiness"]

定义类示例:

class Example
  attr_accessor :status, :description :priority, :tags

  def initialize(status, description, priority, tags)
    @status, @description, @priority = status, description, priority
    @tags = tags.split(', ') # Split tags and store in a list.
  end

end

创建新对象:

list_of_examples = parsed_data.map{|line| Example.new(*line)}
p list_of_examples.first #=> #<Example:0x2212ae8 @priority="High", @description="Eat spaghetti.", @status="TODO", @tags=["food", "happiness"]>
于 2013-11-04T08:43:20.437 回答
0

"TODO | Eat spaghetti. | High | food, happiness".split('|')

=> ["TODO    ", " Eat spaghetti.               ", " High   ", " food, happiness"]

或者

"TODO    | Eat spaghetti.               | High   | food, happiness".scan(/[^|]+/)

=>  ["TODO    ", " Eat spaghetti.               ", " High   ", " food, happiness"]

或者

"TODO    | Eat spaghetti.               | High   | food, happiness".split('|').map{ |i| i.strip }
=> ["TODO", "Eat spaghetti.", "High", "food, happiness"]
于 2013-11-03T12:23:01.283 回答