1

在一个文件中显然存储了一个多行正则表达式,可以YAML::dump(b1)毫无问题地保存。

问题,当 Syck 似乎对多行正则表达式有问题时,我该如何加载它?

b2 = YAML::load(File.open("browserObj.yaml", 'r'))

Syck::TypeError: Invalid Regular expression: "/\\A\\s*\n        ([a-zA-Z][\\-+.a-zA-Z\\d]*):                           (?# 1: scheme)\n        (?:\n           ((?:[\\-_.!~*'()a-zA-Z\\d;?:@&=+$,]|%[a-fA-F\\d]{2})(?:[\\-_.!~*'()a-zA-Z\\d;\\/?:@&=+$,\\[\\]]|%[a-fA-F\\d]{2})*) \\                   (?# 2: opaque)\n        |\n           (?:(?:\n             \\/\\/(?:\n \\                (?:(?:((?:[\\-_.!~*'()a-zA-Z\\d;:&=+$,]|%[a-fA-F\\d]{2})*)@)? \\       (?# 3: userinfo)\n                   (?:((?:(?:[a-zA-Z0-9\\-.]|%\\h\\h)+|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\[(?:(?:[a-fA-F\\d]{1,4}:)*(?:[a-fA-F\\d]{1,4}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(?:(?:[a-fA-F\\d]{1,4}:)*[a-fA-F\\d]{1,4})?::(?:(?:[a-fA-F\\d]{1,4}:)*(?:[a-fA-F\\d]{1,4}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}))?)\\]))(?::(\\d*))?))? (?# 4: host, 5: port)\n               |\n                 ((?:[\\-_.!~*'()a-zA-Z\\d$,;:@&=+]|%[a-fA-F\\d]{2})+) \\                (?# 6: registry)\n               )\n             |\n             (?!\\/\\/)) \\                          (?# XXX: '\\/\\/' is the mark for hostport)\n \\            (\\/(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*)*(?:\\/(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*)*)*)? \\                   (?# 7: path)\n           )(?:\\?((?:[\\-_.!~*'()a-zA-Z\\d;\\/?:@&=+$,\\[\\]]|%[a-fA-F\\d]{2})*))? \\                (?# 8: query)\n        )\n        (?:\\#((?:[\\-_.!~*'()a-zA-Z\\d;\\/?:@&=+$,\\[\\]]|%[a-fA-F\\d]{2})*))? \\                 (?# 9: fragment)\n      \\s*\\z/x"
    from /usr/lib/ruby/1.9.1/syck/rubytypes.rb:284:in `yaml_new'
    from /usr/lib/ruby/1.9.1/syck.rb:135:in `transfer'
    from /usr/lib/ruby/1.9.1/syck.rb:135:in `node_import'
    from /usr/lib/ruby/1.9.1/syck.rb:135:in `load'
    from /usr/lib/ruby/1.9.1/syck.rb:135:in `load'
    from /usr/lib/ruby/1.9.1/syck.rb:146:in `block in load_file'
    from /usr/lib/ruby/1.9.1/syck.rb:145:in `open'
    from /usr/lib/ruby/1.9.1/syck.rb:145:in `load_file'
    from (irb):428
    from /usr/bin/irb:12:in `<main>'

我已经看到有关 ruby​​types.rb 补丁的讨论,但除此之外什么都没有。

我宁愿不切换到 Psych,因为这会打开另一个蠕虫罐:

YAML::ENGINE.yamler = "psych"

# attempt to load from file again
TypeError: can't convert Fixnum into String

这是怎么回事?有希望吗?在此错误使其不透明之前,我对 Syck 和 Psych 以及 YAML 解析的内部都一无所知。

4

2 回答 2

1

您可以将正则表达式存储为字符串。当您从 yaml 文件中读取它时,只需将字符串转换回正则表达式。Regexp.new myregexp_str- 这当然只是一种解决方法。

更新: 或者你可以做一些非常愚蠢的事情并修补心理来处理多行正则表达式。由于修补的方法太长了,所以在这里抓住它。但这里是对真正修补的内容的解释。

module Psych
  module Visitors
    class ToRuby < Psych::Visitors::Visitor
       # ...
      def deserialize
        # ... L75
        when "!ruby/regexp"
        o.value =~ /^\/(.*)\/([mixn]*)$/m # <- notice the added "m" for multiline mode
        # ...
      end
    end
  end
end

使用ruby-1.9.3-p194

于 2012-11-29T13:13:09.663 回答
0

对于潜伏在周围的每个人来说,这看起来像是一个老虫子。我刚刚尝试使用 ruby​​ 1.9.3,我得到了这个: [3] pry(main)> YAML.load(YAML.dump(/asd/m)) => /asd/m

于 2014-10-14T13:47:45.443 回答