简体   繁体   English

计算方法中的正则表达式匹配

[英]counting regexp matches within the method

I have some code like below. 我有一些类似下面的代码。 comment method is called whenever some comment occurs in the html . 每当html出现一些注释时,都会调用comment方法。 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. 我想要的是只打印5因为那是比赛的总数。 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. 只需将puts $count移出循环即可。 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. 一种方法是使您的类在实例变量(例如@count)内部对匹配项的数量进行计数。 Then use attr_reader to create a method allowing you to read its value at the end. 然后使用attr_reader创建一个方法,使您可以在最后读取其值。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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