Ruby word count
1 2 module StringExtensions 3 def words 4 s = self.dup 5 s.gsub!(/\w+/, 'X') 6 s.gsub!(/\W+/, '') 7 s.length 8 end 9 end
DZone Snippets > sikelianos > regex
13463 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
1 2 module StringExtensions 3 def words 4 s = self.dup 5 s.gsub!(/\w+/, 'X') 6 s.gsub!(/\W+/, '') 7 s.length 8 end 9 end
1 2 for i in 1..100 3 print "Now at #{i}. Restart? " 4 retry if gets =~ /^y/i 5 end
1 2 # returns nil if not found or numerical index if found 3 "dog.jpg" =~ /.*(jpg|png|gif)$/ 4 5 validates_format_of :image_url, :with => %r{\.(gif|jpg|png)$}i
1 2 s.gsub!(/\ +/, '-')
1 2 s.gsub!(/\W+/, '')
1 2 s = "TYPE=Exterior RED=119 GREEN=105 BLUE=88 TABLEREQ=PNTTBL-01 TABLE=Primary w/XL GENERICCLR=Beige GENERICCLRCODE=31" 3 s.split(/\s(?=[A-Z]*=)/)
1 2 str = "<html>This and <b>that</b> and <br />and <span class='something'>the other</span>?<html>" 3 puts str.gsub(/<\/?[^>]*>/, "") 4
1 2 class String 3 def count_words 4 n = 0 5 scan(/\b\S+\b/) { n += 1} 6 n 7 end 8 end
1 2 e = assert_raise(RuntimeError) { my_code_that_raises } 3 assert_match(/Error message here/i, e.message)..