当该字符串不包含任何字符时,为什么我的字符串与数组中的某些字符匹配?

编程入门 行业动态 更新时间:2024-10-25 16:22:00
本文介绍了当该字符串不包含任何字符时,为什么我的字符串与数组中的某些字符匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用Rails 5和Ruby 2.4.我不明白为什么下面的匹配.我有一个字符串数组,我想看看我的字符串是否与数组中的任何字符串匹配,所以我尝试了

Using Rails 5 and Ruby 2.4. I can't understand why the below is matching . I have an array of strings and I want to see if my string matches any of those in the array, so I tried this

2.4.0 :021 > SEPARATOR_TOKENS = ["-"] => ["-"] 2.4.0 :022 > data = "W40" => "W40" 2.4.0 :023 > data =~ /[#{Regexp.union(SEPARATOR_TOKENS)}]/ => 0

即使我的字符串中没有-",它仍报告匹配.我该如何纠正?注意,我从根本上简化了此示例,因此使用".include?"不是一种选择,因为我正在做的事情最终将包含正则表达式.

even though there is no "-" in my string, it is reporting a match. HOw do I correct this? Note, I have radically sipmlified this example so using an ".include?" is not an option because what I'm doing will eventually include regular expressions.

推荐答案

Regexp.union已经生成了一个正则表达式,您无需使用/.../

Regexp.union already produces a regular expression, you don't need to use /.../

尝试一下:

data =~ Regexp.union(SEPARATOR_TOKENS)

更多推荐

当该字符串不包含任何字符时,为什么我的字符串与数组中的某些字符匹配?

本文发布于:2023-11-06 09:54:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   不包含   组中

发布评论

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

>www.elefans.com

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