Regexp matching in a block of code

V

Vell

I am trying to create a script that will allow me to compare email
addresses with a list of domains to see if they match. If they don't
match then everything is good to go, but if they do match, I want to
be able to delete them or move them out of the list.

The code that I wrote gives the following when its ran:
TypeError: type mismatch: String given

method =~ in untitled document at line 7
at top level in untitled document at line 7
method each in untitled document at line 5
at top level in untitled document at line 5
method each in untitled document at line 4
at top level in untitled document at line 4
Program exited.

I know my issue is with this particular line: if(add =~
Regexp.escape(comp))
But I am not sure where to find out or how to resolve it

Any help again is appreciated.

Code:

addresses = ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
competitors = ["bar.com", "bar1.com", "bar4.com"]

addresses.each do |add|
competitors.each do |comp|
puts "Checking Competitor: #{comp}"
if(add =~ Regexp.escape(comp))
puts "#{check} matched"
else
puts "No Match"
end
end
end
 
V

Vell

I am trying to create a script that will allow me to compare email
addresses with a list of domains to see if they match.  If they don't
match then everything is good to go, but if they do match, I want to
be able to delete them or move them out of the list.

The code that I wrote gives the following when its ran:
TypeError: type mismatch: String given

method =~       in untitled document at line 7
at top level    in untitled document at line 7
method each     in untitled document at line 5
at top level    in untitled document at line 5
method each     in untitled document at line 4
at top level    in untitled document at line 4
Program exited.

I know my issue is with this particular line: if(add =~
Regexp.escape(comp))
But I am not sure where to find out or how to resolve it

Any help again is appreciated.

Code:

addresses = ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
competitors = ["bar.com", "bar1.com", "bar4.com"]

addresses.each do |add|
  competitors.each do |comp|
    puts "Checking Competitor: #{comp}"
    if(add =~ Regexp.escape(comp))
      puts "#{check} matched"
    else
      puts "No Match"
    end
  end
end

Sorry guys, Messed up on initial insert of the code. Here is the code
as follows:

Code:
addresses = ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
competitors = ["bar.com", "bar1.com", "bar4.com"]
addresses.each do |add|
competitors.each do |comp|
puts "Checking Competitor: #{comp}"
if(add =~ Regexp.escape(comp))
puts "matched"
else
puts "No Match"
end
end
end
 
7

7stud --

Lovell said:
addresses = ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
competitors = ["bar.com", "bar1.com", "bar4.com"]
addresses.each do |add|
competitors.each do |comp|
puts "Checking Competitor: #{comp}"
if(add =~ Regexp.escape(comp))
puts "matched"
else
puts "No Match"
end
end
end

Try this:

add = "(e-mail address removed)"
comp = "bar.com"

if add =~ comp
puts 'matched'
else
puts 'no match'
end
 
K

Karl-Heinz Wild

addresses = ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
competitors = ["bar.com", "bar1.com", "bar4.com"]

addresses.each do |add|
competitors.each do |comp|
puts "Checking Competitor: #{comp}"
if(add =~ Regexp.escape(comp))
puts "#{check} matched"
else
puts "No Match"
end
end
end


As you can see in the documentation Regexp.escape returns a string
and Regexp.new returns an regexp.

If you write

if(add =~ %r { Regexp.escape(comp)) }

or

if(add =~ Regexp.new( Regexp.escape(comp)) )

you code works for me.

- Karl-Heinz
 
K

Karl-Heinz Wild

addresses = ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
competitors = ["bar.com", "bar1.com", "bar4.com"]

addresses.each do |add|
competitors.each do |comp|
puts "Checking Competitor: #{comp}"
if(add =~ Regexp.escape(comp))
puts "#{check} matched"
else
puts "No Match"
end
end
end


As you can see in the documentation Regexp.escape returns a string
and Regexp.new returns an regexp.

If you write

if(add =~ %r { Regexp.escape(comp)) }

or

if(add =~ Regexp.new( Regexp.escape(comp)) )

you code works for me.

You can also write

if Regexp.new( comp ).match( add )
...

- Karl-Heinz
 
W

William James

I am trying to create a script that will allow me to compare email
addresses with a list of domains to see if they match. If they don't
match then everything is good to go, but if they do match, I want to
be able to delete them or move them out of the list.

The code that I wrote gives the following when its ran:
TypeError: type mismatch: String given

method =~ in untitled document at line 7
at top level in untitled document at line 7
method each in untitled document at line 5
at top level in untitled document at line 5
method each in untitled document at line 4
at top level in untitled document at line 4
Program exited.

I know my issue is with this particular line: if(add =~
Regexp.escape(comp))
But I am not sure where to find out or how to resolve it

Any help again is appreciated.

Code:

addresses = ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
competitors = ["bar.com", "bar1.com", "bar4.com"]

addresses.each do |add|
competitors.each do |comp|
puts "Checking Competitor: #{comp}"
if(add =~ Regexp.escape(comp))
puts "#{check} matched"
else
puts "No Match"
end
end
end

Trivial.

E:\>irb --prompt xmp
addresses = ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
==>["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
competitors = ["bar.com", "bar1.com", "bar4.com"]
==>["bar.com", "bar1.com", "bar4.com"]
addresses.map{|x| x.split('@').last } & competitors
==>["bar1.com"]
 
R

Robert Klemme

2008/2/6 said:
I am trying to create a script that will allow me to compare email
addresses with a list of domains to see if they match. If they don't
match then everything is good to go, but if they do match, I want to
be able to delete them or move them out of the list.

The code that I wrote gives the following when its ran:
TypeError: type mismatch: String given

method =~ in untitled document at line 7
at top level in untitled document at line 7
method each in untitled document at line 5
at top level in untitled document at line 5
method each in untitled document at line 4
at top level in untitled document at line 4
Program exited.

I know my issue is with this particular line: if(add =~
Regexp.escape(comp))
But I am not sure where to find out or how to resolve it

Any help again is appreciated.

Code:

addresses = ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
competitors = ["bar.com", "bar1.com", "bar4.com"]

addresses.each do |add|
competitors.each do |comp|
puts "Checking Competitor: #{comp}"
if(add =~ Regexp.escape(comp))
puts "#{check} matched"
else
puts "No Match"
end
end
end

Sorry guys, Messed up on initial insert of the code. Here is the code
as follows:

Code:
addresses = ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
competitors = ["bar.com", "bar1.com", "bar4.com"]
addresses.each do |add|
competitors.each do |comp|
puts "Checking Competitor: #{comp}"
if(add =~ Regexp.escape(comp))
puts "matched"
else
puts "No Match"
end
end
end

There are a few things to say about this approach. First, it seems no
regular expression is needed here - at least not for matching.
Second, using Set is significantly more efficient. Here's one way to
do it:

competitors = ["bar.com", "bar1.com", "bar4.com"].to_set
addresses = ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]
dirty, clean = addresses.partition {|adr| competitors.include?
adr[/@(\S+)/, 1].downcase}

Now, if you are reading a large number of addresses from a file, you could do

competitors = ["bar.com", "bar1.com", "bar4.com"].to_set
dirty = []
clean = []

File.foreach "adresses.txt" do |line|
adr = line[/\S+@\S+/] # this should be improved
(competitors.include? adr[/@(\S+)/, 1].downcase ?
dirty : clean) << adr
end

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top