我正在做以下事情:
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添加到每条规则的末尾?
答案 0 :(得分:9)
使用选项“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/
答案 1 :(得分:3)
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
答案 2 :(得分:1)
您可以将电子邮件地址设为小写。
答案 3 :(得分:1)
我在您当前的实现中看到的一个问题是它会匹配像luvesouthwestlcom.com这样的域,因为.
匹配任何字符。你可以通过做这样的事情来逃避你正在使用的所有网址来解决这个问题:
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'
。如果您想要完全匹配,可以在下输入输入域后使用==
。 e.g。
domains.any? { |rule| domain == rule }
答案 4 :(得分:1)
无需使用正则表达式进行简单的字符串比较。
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进行不区分大小写的比较。
答案 5 :(得分:0)
您可以将规则作为简单字符串传递并动态构建正则表达式:
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) }