Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regexp matching in a block of code

Reply
Thread Tools

Regexp matching in a block of code

 
 
Vell
Guest
Posts: n/a
 
      02-06-2008
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 = ["", "", ""]
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
 
Reply With Quote
 
 
 
 
Vell
Guest
Posts: n/a
 
      02-06-2008
On Feb 5, 10:00*pm, Vell <lovell.mcilw...@gmail.com> wrote:
> 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 = ["f...@bar1.com", "f...@bar2.com", "f...@bar3.com"]
> 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 = ["f...@bar1.com", "f...@bar2.com", "f...@bar3.com"]
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
 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      02-06-2008
Lovell Mcilwain wrote:
> addresses = ["f...@bar1.com", "f...@bar2.com", "f...@bar3.com"]
> 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 = ""
comp = "bar.com"

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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Karl-Heinz Wild
Guest
Posts: n/a
 
      02-06-2008

On 06.02.2008, at 04:04, Vell wrote:

> addresses = ["", "", ""]
> 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





 
Reply With Quote
 
Karl-Heinz Wild
Guest
Posts: n/a
 
      02-06-2008
On 06.02.2008, at 08:26, Karl-Heinz Wild wrote:

> On 06.02.2008, at 04:04, Vell wrote:
>
>> addresses = ["", "", ""]
>> 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


 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      02-06-2008
On Feb 5, 9:00 pm, Vell <lovell.mcilw...@gmail.com> wrote:
> 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 = ["f...@bar1.com", "f...@bar2.com", "f...@bar3.com"]
> 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 = ["f...@bar1.com", "f...@bar2.com", "f...@bar3.com"]
==>["f...@bar1.com", "f...@bar2.com", "f...@bar3.com"]
competitors = ["bar.com", "bar1.com", "bar4.com"]
==>["bar.com", "bar1.com", "bar4.com"]
addresses.map{|x| x.split('@').last } & competitors
==>["bar1.com"]
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-06-2008
2008/2/6, Vell <>:
> On Feb 5, 10:00 pm, Vell <lovell.mcilw...@gmail.com> wrote:
> > 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 = ["f...@bar1.com", "f...@bar2.com", "f...@bar3.com"]
> > 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 = ["f...@bar1.com", "f...@bar2.com", "f...@bar3.com"]
> 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 = ["f...@bar1.com", "f...@bar2.com", "f...@bar3.com"]
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

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
g++ throws: error: no matching function for call to `block::block()' Alvin C++ 8 12-03-2006 10:33 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
regex matching rst code block? Edward K. Ream Python 4 09-03-2006 04:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57