daily pastebin goal
5%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /opt/local/bin/ruby1.9 -w
  2. # -*- coding: utf-8 -*-
  3.  
  4. # get_tag_and_class.rb: extract tag and class names from a HTML document.
  5.  
  6. regex = Regexp.compile('<(\w+)[^<>]*class=[\'\"]([^\'\"]+)[\'\"][^<>]*>')
  7.  
  8. result = Array.new
  9. Tuple = Struct.new(:line_number, :tag, :class)
  10.  
  11. line_number = 0
  12. STDIN.each { |line|
  13.   line_number += 1
  14.   offset = 0
  15.   while offset < (line.length - 1)
  16.     md = regex.match(line, offset)
  17.     if md
  18.       result.push Tuple.new(line_number, md[1], md[2])
  19.       offset = md.offset(0)[1] + 1
  20.     else
  21.       break
  22.     end
  23.   end
  24. }
  25.  
  26. result.each { |t|
  27.   STDOUT.puts "#{t.line_number}: #{t.tag}.#{t.class}"
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top