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

标签 ruby docker

我正在尝试使用NLP工具(“别名提取工具”)来处理某些日语文本文件。顺便说一句,我在docker容器上运行它。

  • docker操作系统镜像:Ubuntu 18.04
  • docker版本:18.06.0-ce
  • ruby 版本: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
    

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

    最佳答案

    不要将/[^]+/用作正则表达式:这是无效的。

    Ruby 1.8.x1正则表达式引擎将[视为字符类的开始,并认为]+是字符类中的字符-视为已处理字符的]可以遵循^ -negation而无需转义1 。

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

    上面的警告提供了一个指示符:“警告:字符类具有]'且没有转义”,表明]由于上述原因未关闭字符类。

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

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

    它实际上取决于正则表达式的实际意图。只需要以有效格式编写正则表达式即可。

    关于ruby - 正则表达式过早结束:/[^] +/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51941153/

    相关文章:

    docker - Deis Batch作业

    linux - 连接到 docker 容器中的特定 shell 实例?

    ruby (石头剪刀布)

    ruby-on-rails - 处理旧数据库中的列名 'hash'

    ruby : How can I detect/intelligently guess the delimiter used in a CSV file?

    amazon-web-services - AWS Batch 无法启动 Dockerfile -standard_init_linux.go :219: exec user process caused: exec format error

    python - 从 R 或 python 运行 yaml 文件以进行并行 Selenium 测试

    ruby-on-rails - 管理中的多项选择

    java - 从 Scala(和 Java)访问 DRb 对象(例如 Ruby 队列)的最佳方式是什么?

    适用于 Mac 的 Docker : Host network and port publishing