【问题标题】:Why does this regular expression return incorrect values?为什么这个正则表达式返回不正确的值?
【发布时间】:2013-03-31 00:29:36
【问题描述】:

假设我有一个字符串如下:

"auto: true; server: false;"

...我想要一个正则表达式来创建这些设置的哈希值。我有以下代码:

# class Configurer...
def spit(path = "", *args)
  spat = Hash.new
  if File.file?(path)
    # Parse file
  else
    args.each do |arg|
      begin
        if path.include? arg + ":"
          strip = path.match(/#{arg}:\s(.*);/)
          spat[arg] = strip[1]
        end
      rescue
        return "Error when parsing '#{arg}' in direct input."
      end
    end
  end
  spat
end

当类似:

config = Configurer.new
puts config.spit("auto: true; server: false;", "auto", "server")

...运行,输出是不正确的散列:

# => {"auto"=>"true; server: false", "server"=>"false"}

这是为什么呢?当我parse a file (line by line) 并使用相同的正则表达式时,我得到了所需的哈希值。为什么这种方法不是这样?

【问题讨论】:

  • 如果设置为strip[0]会怎样?
  • @Linuxios 如果我这样做,它将返回原始字符串,1MatchData 数组中的第二项。该项目是正则表达式匹配的原因。

标签: ruby regex


【解决方案1】:

改用non-greedy repetition

/#{arg}:\s(.*?);/

【讨论】:

  • 效果很好!你认为我也应该在逐行解析中使用它吗? Source code here.
  • 它应该没有区别 - 只要值不包含分号。也许你应该只在有人将它复制到它应该匹配多个键值对的地方的情况下使用它......
  • 或者你可以使用/([^;])*/
  • @Rein:是的,对于这个简单的案例来说基本上是一样的。也许更具表现力:-)
  • @Bergi Non-greedy 也稍微慢一些,但通常不会很慢。无论如何,如果您使用正则表达式来提高速度,那么您做错了;)
猜你喜欢
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
  • 2014-04-21
  • 1970-01-01
  • 1970-01-01
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多