如何使用正则表达式搜索数组? [英] How to search through array with Regex?

查看:46
本文介绍了如何使用正则表达式搜索数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图将电子邮件与电子邮件域匹配(即 bob@test 与 @test.com).电子邮件数组和电子邮件域都在不同的数组中.现在我正在尝试以下操作:

So I am trying to match email to email domains (ie bob@test to @test.com). Both the array of emails and email domains are in separate arrays. Right now I am trying the following:

@Domains.each do |domain|
    if @Emails.include?(/.*/ + domain) then 
        #do stuff
    end
end

这显然是在抛出错误,我找不到其他解决方案.有什么想法吗?

That is obviously throwing errors and I cannot find another solution. Any ideas?

推荐答案

不清楚您想用正则表达式做什么.这是使其运行的最低限度的修复.它不使用正则表达式.

It is not clear what you are trying to do with a regex. This is a minimum fix to make it run. It does not use a regex.

@Domains.each do |domain|
  if @Emails.any?{|email| email.end_with?(domain)} 
    #do stuff
  end
end

这篇关于如何使用正则表达式搜索数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆