我正在做以下事情:
email = 'bob@luv.southwest.com'
domain_rules = [/craigslist.org/, /evite.com/, /ziprealty.com/, /alleyinsider.com/, /fedexkinkos.com/, /luv.southwest.com/, /fastsigns.com/, /experts-exchange.com/, /feedburner.com/]
user, domain = email.split('@')
domain_rules.each { |rule| return true if !domain.match(rule).nil? }
问题是这是区分大小写的。有没有一种方法可以使所有的大小写无关,而不必在每条规则的末尾添加/i ?
使用选项"i"(忽略大小写)
domain_rules = [
/craigslist.org/i,
/evite.com/i,
/ziprealty.com/i,
/alleyinsider.com/i,
/fedexkinkos.com/i,
/luv.southwest.com/i,
/fastsigns.com/i,
/experts-exchange.com/i,
/feedburner.com/i
]
在这里测试…http://rubular.com/
downcase
电子邮件&首先要匹配的域,然后find_all
regexp匹配。
您可以使用find
只检索第一个匹配的"规则"。
email = 'bob@luv.southwest.com'
domain_rules = [/craigslist.org/, /evite.com/, /ziprealty.com/, /alleyinsider.com/, /fedexkinkos.com/, /luv.southwest.com/, /fastsigns.com/, /experts-exchange.com/, /feedburner.com/]
user, domain = email.split('@').collect { |s| s.downcase }
p domain_rules.find_all { |rule| domain[rule] }
实际上也不需要Regexp:
email = 'bob@luv.southwest.com'
matchable_domains = %w{ craigslist.org evite.com ziprealty.com alleyinsider.com fedexkinkos.com luv.southwest.com fastsigns.com experts-exchange.com feedburner.com }
user, domain = email.downcase.split('@')
p matchable_domains.find_all { |rule| matchable_domains.include?(domain) }
或者,您可以只做Regexp:
email = 'bob@luv.southwest.com'
regexp = /[A-Z0-9._%+-]+@(craigslist.org|evite.com|ziprealty.com|alleyinsider.com|fedexkinkos.com|luv.southwest.com|fastsigns.com|experts-exchange.com|feedburner.com)/
p regexp === email # => true
p regexp.match(email) # => #<MatchData "bob@luv.southwest.com" 1:"bob" 2:"luv.southwest.com">il
不需要使用正则表达式进行简单的字符串比较。
email = 'bob@luv.southwest.com'
domains = %w(CraigsList.org evite.com ZiPreAltY.com alleyinsider.com fedexkinkos.com luv.southwest.com fastsigns.com experts-exchange.com feedburner.com)
user, user_domain = email.split('@')
p domains.any? { |domain| domain.casecmp(user_domain).zero? }
String#casecmp
不区分大小写。
你可以把电子邮件地址改成小写。
我看到你目前的实现的一个问题是,它将匹配域像luvesouthwestlcom.com,因为.
匹配任何字符。你可以通过转义你正在使用的所有url来处理这个问题,像这样做:
email = 'bob@luv.southwest.com'
domains = %w[craigslist.org evite.com ziprealty.com alleyinsider.com fedexkinkos.com luv.southwest.com fastsigns.com experts-exchange.com feedburner.com]
domain_rules = domains.map{|d| /#{Regexp.escape(d)}/i }
user, domain = email.split('@')
domain_rules.any? { |rule| domain.match(rule) }
同样,如果你只是在寻找精确匹配,你真的不需要正则表达式,你可以检查一下电子邮件的域名是否包含你正在寻找的字符串之一。
email = 'bob@luv.southwest.com'
domains = %w[craigslist.org evite.com ziprealty.com alleyinsider.com fedexkinkos.com luv.southwest.com fastsigns.com experts-exchange.com feedburner.com]
user, domain = email.split('@')
domain.downcase! # lower cases the string in place
domains.any? { |rule| domain.include?(rule) }
这两种方法的问题是它们将匹配任何具有精确字符串的内容,因此'craigslist.org'
将匹配'nyc.craiglist.org'
和'craigslist.org.uk'
。如果您想要精确匹配,您可以在输入域降级后使用==
。例如
domains.any? { |rule| domain == rule }
您可以将规则作为简单字符串传递,并动态构造正则表达式:
email = 'bob@luv.southwest.com'
domains = %w(craigslist.org evite.com ziprealty.com) # etc
user, domain = email.split('@').collect { |s| s.downcase }
p domains.any? { |d| domain.match(/#{d}/i) }