内插正则表达式中char类的提前结束(Premature end of char class in interpolated regex)

编程入门 行业动态 更新时间:2024-10-27 20:39:53
内插正则表达式中char类的提前结束(Premature end of char class in interpolated regex)

我似乎无法解决这个问题,希望有人能帮助:

Nilfacs是从哈希中抽取的一串字符串。

对于这一行:

looping_finaltext = finaltext.reject {|sentence| nilfacs.any? {|fac| sentence =~ /#{(fac)}/i}}

我得到了以下错误: warning: character class has ']' without escape: /[[]]/ block (2 levels) in <main>': premature end of char-class: /[[]]/i (RegexpError) warning: character class has ']' without escape: /[[]]/和block (2 levels) in <main>': premature end of char-class: /[[]]/i (RegexpError)

所有的字符串都只是普通的单词(如“条件”),并且不包含需要转义的字符。

这是否意味着一些意想不到的事情正在作为一个字符串输入到数组中? 或者在这一行中我的语法有什么问题?

I can't seem to solve this issue, hope someone can help:

Nilfacs is an array of strings pulled from a hash.

For this line:

looping_finaltext = finaltext.reject {|sentence| nilfacs.any? {|fac| sentence =~ /#{(fac)}/i}}

I get the following errors: warning: character class has ']' without escape: /[[]]/ and block (2 levels) in <main>': premature end of char-class: /[[]]/i (RegexpError)

All of the strings are just normal words (like "condition") and do not contain characters that should need to be escaped.

Is this an indication that something unanticipated is being fed into the array as a string? Or is there something wrong with my syntax in this line?

最满意答案

这是否意味着一些意想不到的事情正在作为一个字符串输入到数组中?

是的,就是这样。 我期望你有嵌套的数组,并且在那里有一个空数组[[]]的数组,它的to_s表达式产生你找到的结果。

当您在正则表达式中使用插值时,源中的字符将被视为正则表达式中的字符。 就像/b[/不是一个有效的正则表达式,所以foo="b["; bar=/#{foo}/ foo="b["; bar=/#{foo}/无效。

nilfacs = [ "a[]", "b[", "c]", [[]] ] nilfacs.each do |fac| begin p /#{fac}/ rescue RegexpError=>e puts e end end #=> empty char-class: /a[]/ #=> premature end of char-class: /b[/ #=> /c]/ #=> warning: regular expression has ']' without escape: /[[]]/ #=> premature end of char-class: /[[]]/

如果要将字符串用作文字字符,则需要使用Regexp.escape :

nilfacs.each do |fac| p /#{Regexp.escape fac}/ end #=> /a\[\]/ #=> /b\[/ #=> /c\]/

或者,您可能希望使用Regexp.union从您的数组中创建一个与其中所有文字字符串匹配的正则表达式:

rejects = %w[dog cat] re = Regexp.new(Regexp.union(rejects).source,'i') #=> /dog|cat/i looping_finaltext = finaltext.reject{ |sentence| sentence=~re }

Is this an indication that something unanticipated is being fed into the array as a string?

Yes, that is it exactly. I expect that you have nested arrays and somewhere in there you have an array of an empty array [[]] whose to_s representation produces the result you found.

When you use interpolation in a regex literal the characters in your source are treated as they would be in regex. Just as /b[/ is not a valid regular expression, so foo="b["; bar=/#{foo}/ is not valid.

nilfacs = [ "a[]", "b[", "c]", [[]] ] nilfacs.each do |fac| begin p /#{fac}/ rescue RegexpError=>e puts e end end #=> empty char-class: /a[]/ #=> premature end of char-class: /b[/ #=> /c]/ #=> warning: regular expression has ']' without escape: /[[]]/ #=> premature end of char-class: /[[]]/

If you want to use your strings as literal characters, you want to use Regexp.escape:

nilfacs.each do |fac| p /#{Regexp.escape fac}/ end #=> /a\[\]/ #=> /b\[/ #=> /c\]/

Alternatively, you may want to use Regexp.union to create a single regexp from your array that matches all the literal strings therein:

rejects = %w[dog cat] re = Regexp.new(Regexp.union(rejects).source,'i') #=> /dog|cat/i looping_finaltext = finaltext.reject{ |sentence| sentence=~re }

更多推荐

本文发布于:2023-08-02 20:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379711.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内插   结束   正则表达式   char   regex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!