How to make this regular expression rule rule insensitive

I do the following:

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? } 

The problem is that it is case sensitive. Is there a way to do this all case-insensitively, without having to add / i to the end of each individual rule?

+4
source share
6 answers

downcase the email address and domain that you want to map first, then find_all regexp matches.

You can use find only to get the first matching "rule".

 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] } 

Also there is no need for 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) } 

Or you can do ONLY 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 
+3
source

Use parameter "i" (ignore case)

 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 ] 

test it here ... http://rubular.com/

+8
source

You can simply enter the email address in lower case.

+1
source

One problem that I see with your current implementation is that it will correspond to domains like luvesouthwestlcom.com, because . matches any character. You can handle this by avoiding the whole URL that you are using, doing something like this:

 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) } 

Also, if you are only looking for exact matches, you do not need regular expressions, and you can simply check if the email domain includes one of the strings you are looking for.

 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) } 

The problem with any of them is that they will match anything with an exact string, so 'craigslist.org' will match 'nyc.craiglist.org' and 'craigslist.org.uk' . If you need exact matches, you can simply use == after deleting your input domain. eg.

 domains.any? { |rule| domain == rule } 
+1
source

You do not need to use regular expressions to easily compare strings.

 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 does a case insensitive comparison.

+1
source

You can pass the rules as simple strings and build a regex on the fly:

 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) } 
0
source

All Articles