Regexp matching in a block of code

Discussion in 'Ruby' started by Vell, Feb 6, 2008.

  1. Vell

    Vell Guest

    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
     
    Vell, Feb 6, 2008
    #1
    1. Advertising

  2. Vell

    Vell Guest

    On Feb 5, 10:00 pm, Vell <> 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 = ["", "", ""]
    > 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 = ["", "", ""]
    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
     
    Vell, Feb 6, 2008
    #2
    1. Advertising

  3. Vell

    7stud -- Guest

    Lovell Mcilwain 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 "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/.
     
    7stud --, Feb 6, 2008
    #3
  4. 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
     
    Karl-Heinz Wild, Feb 6, 2008
    #4
  5. 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
     
    Karl-Heinz Wild, Feb 6, 2008
    #5
  6. On Feb 5, 9:00 pm, Vell <> 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 = ["", "", ""]
    > 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 = ["", "", ""]
    ==>["", "", ""]
    competitors = ["bar.com", "bar1.com", "bar4.com"]
    ==>["bar.com", "bar1.com", "bar4.com"]
    addresses.map{|x| x.split('@').last } & competitors
    ==>["bar1.com"]
     
    William James, Feb 6, 2008
    #6
  7. 2008/2/6, Vell <>:
    > On Feb 5, 10:00 pm, Vell <> 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 = ["", "", ""]
    > > 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 = ["", "", ""]
    > 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 = ["", "", ""]
    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
     
    Robert Klemme, Feb 6, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward K. Ream

    regex matching rst code block?

    Edward K. Ream, Sep 3, 2006, in forum: Python
    Replies:
    4
    Views:
    394
    Edward K. Ream
    Sep 3, 2006
  2. morrell
    Replies:
    1
    Views:
    969
    roy axenov
    Oct 10, 2006
  3. Alvin
    Replies:
    8
    Views:
    991
  4. Joao Silva
    Replies:
    16
    Views:
    368
    7stud --
    Aug 21, 2009
  5. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    239
    Marc Bissonnette
    Jan 13, 2004
Loading...

Share This Page