简体   繁体   中英

counting regexp matches within the method

I have some code like below. comment method is called whenever some comment occurs in the html . Then, I am doing a regexp match, I want to count the number of matches within the parsed comments. Its printing like below

1
2
3
4
5

what I want is to just print 5 because thats the total number of matches. can someone help pls.

class PlainTextExtractor < Nokogiri::XML::SAX::Document
  def comment(string)
    # I am defining some regexp here 
    m = Regexp.new(re, Regexp::IGNORECASE);
    if m.match(string)
      $count += 1
      puts $count 
    end
  end
end

parser = Nokogiri::HTML::SAX::Parser.new(PlainTextExtractor.new)
parser.parse_memory(html)

Just move your puts $count out of the loop. You can put it at the end, after you call the parser.

If you are only interested in the number of matches you can do

m = Regexp.new(re, Regexp::IGNORECASE);
puts string.scan(m).length

One way is to make your class count the number of matches internally in an instance variable, eg @count. Then use attr_reader to create a method allowing you to read its value at the end. Also you don't need a global variable. Example (not tested):

class PlainTextExtractor < Nokogiri::XML::SAX::Document
  attr_reader :count
  def comment(string)
    # I am defining some regexp here 
    m = Regexp.new(re, Regexp::IGNORECASE);
    if m.match(string)
      @count += 1
    end
  end
end

pt_extractor = PlainTextExtractor.new
parser = Nokogiri::HTML::SAX::Parser.new(pt_extractor)
parser.parse_memory(html)
puts pt_extractor.count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM