2

I am trying to match a array of emails to an array of domains. I am currently running a problem with my regex:

empty range in char class: /.*["@email-email.com"]$/ (RegexpError).

Here is what I am trying:

@emails.each do |email|
    @domains.each do |domain|
        if email =~ /.*#{domain}/
            puts domain
        end
    end
end
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
user3427228
  • 25
  • 1
  • 5
  • You need to escape the square brackets and your dash, these characters have meaning within regular expressions. Take a look at this question as well: http://stackoverflow.com/questions/13161903/regular-expression-empty-range-in-char-class-error – Hunter McMillen Mar 17 '14 at 01:44
  • 1
    Hmm, escaped all the symbols which got rid of the error. However I did not get any matches. Is the [" actually showing up in the regex? If so should I convert it to a string? – user3427228 Mar 17 '14 at 01:56
  • Yes, if you have the escaped square brackets in your regular expression then it is assumed that will be in what you are matching. What are you trying to match with that pattern ? – Hunter McMillen Mar 17 '14 at 01:58
  • I don't have any brackets in my regex, nor in my source file so I don't know where they are coming from. I want to match email to domains. (ie: bob@test.com to @test.com – user3427228 Mar 17 '14 at 02:02
  • 2
    This, `/.*["@email-email.com"]$/`, is the pattern you have posted in your question. – Hunter McMillen Mar 17 '14 at 02:03

1 Answers1

0

The below would generate a hash of domain as the key and array of matching emails

@domains.each_with_object({}) { |domain,hash| hash[domain] = emails.select { |email| email.ends_with(domain) } }

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
bjhaid
  • 9,592
  • 2
  • 37
  • 47