如何通过与常规的前pressions数组搜索? [英] How to search through array with regular expressions?

查看:120
本文介绍了如何通过与常规的前pressions数组搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,这是我所要完成的任务更大规模的一个小例子。我需要通过电子邮件的数组来搜索并匹配电子邮件域的另一个数组。所以,如果他们存在打印出来的电子邮件地址。不过,我不能想出一个办法做到这一点。这是我走到这一步,但它是没有好处的。

 电子邮件=jim@test.com,bob@test.com,dan@apple.com
域=@ test.com,@ google.comemail.each做|电子邮件|
   如果电子邮件== /.*#{domain}/
      把电子邮件
   结束
结束


解决方案

您应该使用的 的grep

 电子邮件=jim@test.com,bob@test.com,dan@apple.com
域=@ test.com,@ google.com
章= Regexp.union(域)
email.grep(/(#{}章)$ /)做|比赛|
    把比赛
结束#>> jim@test.com
#>> bob@test.com

Hi this is a small example of what I want to accomplish on a larger scale. I need to search through an array of emails and match them to another array of email domains. So if they exists print out the email addresses. However I cannot figure out a way to do this. This is what I got so far, but it is no good.

email = "jim@test.com", "bob@test.com", "dan@apple.com"
domain = "@test.com", "@google.com"

email.each do |email|
   if email == /.*#{domain}/ 
      puts email
   end
end

解决方案

You should use grep :

email = "jim@test.com", "bob@test.com", "dan@apple.com"
domain = "@test.com", "@google.com"
reg = Regexp.union(domain)
email.grep(/(#{reg})$/) do |match|
    puts match
end

# >> jim@test.com
# >> bob@test.com

这篇关于如何通过与常规的前pressions数组搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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