如何解析文本以获取所有提及的内容?

时间:2015-01-13 13:58:41

标签: ruby-on-rails ruby regex

我想解析一个文本,以便从该文本中获得一些提及的内容:

class Mentionee
  attr_reader :id, :display_name

  def initialize(id:, display_name:)
    @id = id
    @display_name = display_name
  end

  def self.parse(text)
     # ???
  end
end

mentionees = Mentionee.parse('[1:John C.] [2: Smith X.] you are awesome!')
mentioneess[0].id            # => '1'
mentioneess[0].display_name  # => 'John C.'
mentioneess[1].id            # => '2'
mentioneess[1].display_name  # => 'Smith X.'

2 个答案:

答案 0 :(得分:3)

我认为这会对你有帮助。

> '[1:John C.] [2: Smith X.] you are awesome!'.scan(/(?<=\[)(\d+)(?=:\s*([^\]]+))/)
=> [["1", "John C."], ["2", "Smith X."]]

答案 1 :(得分:1)

如果我理解你要解析传递给方法parse

的文本
  def self.parse(text)
    text.scan(/\[(.*?):(.*?)\]/).map do |e|
      {id: e[0], display_name: e[1]}
    end
  end

将产生:

[
  {id: "1", display_name: "John C."},
  {id: "2", display_name: "Smith X."}
]

你可以像你描述的那样使用

mentionees = Mentionee.parse('[1:John C.] [2: Smith X.] you are awesome!')
mentioneess[0][:id]            # => 1
mentioneess[0][:display_name]  # => 'John C.'
mentioneess[1][:id]            # => 2
mentioneess[1][:display_name]  # => 'Smith X.'