正则表达式过早结束:/ [^] + /

时间:2018-08-21 03:25:49

标签: ruby docker

我正在尝试使用NLP工具[hyponymy extracting tool]处理某些日语文本文件。顺便说一下,我在docker容器上运行它。

  • docker操作系统映像:Ubuntu 18.04
  • docker版本:18.06.0-ce
  • 红宝石版本:1.8.7-p375 ​​
  • rbenv版本:1.1.1-39-g59785f6

jcode.rb

11:def _regex_quote(str)
str.gsub(/(\\[\[\]\-\\])|\\(.)|([\[\]\\])/) do
  $1 || $2 || '\\' + $3
end
end
 private :_regex_quote
151:def delete!(del)
return nil if del == ""
self.gsub!(DeletePatternCache[del] ||= /[#{_regex_quote(del)}]+/, '')
end

del_mark.rb

103:REG_PAREN = /[#{Regexp.quote((PAREN.keys+PAREN.values).join(''))}]/
    REG_BPAREN = /^([#{Regexp.quote(PAREN.keys.join(''))}])/
    (main.opts).on('-p','--end-paren','先頭と末尾の括弧のみ取り除く'){|d|
      options[:pat] = Array.new if options[:pat] == FULL_SET
      options[:pat].push(REG_BPAREN)
    }
    (main.opts).on('-P','--only-paren','括弧のみ_に置き換え'){|d| 
      options[:pat] = Array.new if options[:pat] == FULL_SET
      PAREN_OUT= '_'
      options[:pat].push(REG_PAREN)
   }
   #(main.opts).on('-d','--date'){
   #options[:pat].push(REG_DATE)
   #}
  (main.opts).on('-r','--remove'){
    options[:remove]=true
  }

    main.option
130:(ARGV+[$stdin]).each{|file|
    fi = file
    if file != $stdin
      fi = open(file)
    end
    main.input = fi 
    #puts "F:#{file}";
   138: main.exec{|line|
      unless line 
        main.is_line_write = false
        next
      end
  163:line.each_char{|char|
  #puts "C:#{char}"
  165:options[:pat].each{|pat|
        if pat == REG_BOU || pat == REG_BPAREN
        elsif pat == REG_PAREN
          char =char.sub(REG_PAREN,PAREN_OUT)
        else 
          reg = check_reg(char,pat)
          if reg
       # line.delete
       173:line.delete!("#{char}")
      # line.delete!("#{char[0,1]}\s?")
      # line = line[0..nchar-1] + line[nchar+char.size..line.size-1]
      # line.delete!(Regexp.quote(char))
        line.gsub!(reg,'')
        flag = true
        break
      end
    end

Cut.rb

if @is_data_array
    cd = Array.new
    @column.each{|c|
      cd.push(data[c])
    }
    func.call(cd)
  else
158: @column.each{|c|
      #puts "LINE:#{c},#{data[c]}";
  160: ret = @negrect_pattern == nil || !(data[c] =~ @negrect_pattern ) ? func.call(data[c]) : negrect(data[c]){|d| func.call(d)} 
      unless ret # if ret is nil
        is_write = false
        next
      end
      data[c] = ret
    }
  endenter code here

启动工具后,出现错误消息:

/root/.rbenv/versions/1.8.7-p375/lib/ruby/1.8/jcode.rb:153: warning: character class has `]' without escape
/root/.rbenv/versions/1.8.7-p375/lib/ruby/1.8/jcode.rb:153:in `delete!': premature end of regular expression: /[^]+/ (RegexpError)
from /ex-hyponymy-1.0/script/lib/del_mark.rb:173
from /ex-hyponymy-1.0/script/lib/del_mark.rb:165:in `each'
from /ex-hyponymy-1.0/script/lib/del_mark.rb:165
from /root/.rbenv/versions/1.8.7-p375/lib/ruby/1.8/jcode.rb:212:in `each_char'
from /root/.rbenv/versions/1.8.7-p375/lib/ruby/1.8/jcode.rb:211:in `scan'
from /root/.rbenv/versions/1.8.7-p375/lib/ruby/1.8/jcode.rb:211:in `each_char'
from /ex-hyponymy-1.0/script/lib/del_mark.rb:163
from /ex-hyponymy-1.0/script/lib/Cut.rb:160:in `call'
from /ex-hyponymy-1.0/script/lib/Cut.rb:160:in `exec'
from /ex-hyponymy-1.0/script/lib/Cut.rb:158:in `each'
from /ex-hyponymy-1.0/script/lib/Cut.rb:158:in `exec'
from /ex-hyponymy-1.0/script/lib/del_mark.rb:138
from /ex-hyponymy-1.0/script/lib/del_mark.rb:130:in `each'
from /ex-hyponymy-1.0/script/lib/del_mark.rb:130

我不明白这一点。我希望有人能帮助我。

1 个答案:

答案 0 :(得分:3)

请勿将/[^]+/用作正则表达式:它是无效的。

Ruby 1.8.x 1 正则表达式引擎将[视为字符类的开始,并认为]和{ {1}}是字符类内的字符 -允许将被视为文字字符的+跟随]的否定,而不能转义 1 < / sup>。

因此,字符类没有关闭-并引发了“ [正则表达式]的“过早结束”错误。

上面的警告提供了指标:“警告:角色类具有]'且没有转义”,表明^没有完成 < / em>由于上述原因,关闭了字符类。

1 这是“旧Ruby”规则。 Ruby 2+(甚至可能是1.9+?)具有不同的正则表达式规则,并引发了“空字符类”错误,这不那么神奇,也不太混乱。现在可能是更新所用Ruby版本的好时机。


也许不需要字符取反(]/[+^]/)?还是在字符类(/[\^]+/)内还有其他字符?还是要匹配除括号和加号(/[^f]+/;或Ruby 2+中的/[^]+]/)之外的任何单个字符?或者..

这真的取决于正则表达式的 actual 意图是什么..只需将其以有效格式编写即可。