在寻找子串时,为什么匹配比扫描更好?

时间:2017-03-19 05:55:31

标签: ruby regex

为什么scan无效match?我喜欢scan,它返回一个方便的数组。

这是方法,调用和放置,我不知道为什么:

def getEmail(url)
        begin
        @@src = getUrl(url).encode("UTF-8")
        @emails = @@src.to_s.scan(/([a-z0-9_\.-]+@[a-z0-9_\.-]+\.[a-z\.]{2,6})/)
        rescue Exception => e
            puts e
        end
        @emails = Array.new

        pp @email
        puts "^^^^ SCAN"

        /([a-z0-9_\.-]+@[a-z0-9_\.-]+\.[a-z\.]{2,6})/.match(@@src.to_s)
        pp Regexp.last_match
        puts "^^^^ MATCH"

        begin
        @@src = getUrl(url + "/contacts")
        @emails = @@src.to_s.scan(/([a-z0-9_\.-]+@[a-z0-9_\.-]+\.[a-z\.]{2,6})/)
        pp @email
        puts "^^^^ SCAN"
        rescue Exception => e
            puts e
        end

        /([a-z0-9_\.-]+@[a-z0-9_\.-]+\.[a-z\.]{2,6})/.match(@@src.to_s)
        pp Regexp.last_match
        puts "^^^^ MATCH"
    end     


@t = Tools.new
@t.getEmail("http://ligasound.designb2b.ru")

结果:

== request http://ligasound.designb2b.ru
nil
^^^^ SCAN
#<MatchData "liga@mail.ru" 1:"liga@mail.ru">
^^^^ MATCH
 == request http://ligasound.designb2b.ru/contacts
nil
^^^^ SCAN
#<MatchData "liga@mail.ru" 1:"liga@mail.ru">
^^^^ MATCH

为什么呢?

0 个答案:

没有答案