如何阅读格式化文本?

时间:2013-11-03 12:07:54

标签: ruby text-formatting

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

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 个答案:

答案 0 :(得分:1)

您可能希望使用string.split而不是regex.match 请参阅documentation

答案 1 :(得分:0)

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

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

OR

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

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

OR

"TODO    | Eat spaghetti.               | High   | food, happiness".split('|').map{ |i| i.strip }
=> ["TODO", "Eat spaghetti.", "High", "food, happiness"]

答案 2 :(得分: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"]
]

答案 3 :(得分: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"]>