Skip to content

Instantly share code, notes, and snippets.

Created July 27, 2015 06:00
Show Gist options
  • Save anonymous/d1c7b32b89af36d7789a to your computer and use it in GitHub Desktop.
Save anonymous/d1c7b32b89af36d7789a to your computer and use it in GitHub Desktop.
before_ten=%w{one two three four five six seven eight nine}
before_twenty=%w{ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen}
before_twenty=before_ten+before_twenty
smalls=["twen","thir","for","fif","six","seven","eigh","nine"].map{|t|t+"ty"}
smalls=smalls.join("|")
before_twenty=before_twenty.join("|")
before_ten=before_ten.join("|")
places=%w{thousand million billion trillion}
reg=[]
places.insert(0,"")
places.each.with_index do |t,idx|
out=""
(0...idx+1).to_a.reverse.each do |i|
out+="(?:(?:#{before_ten})hundred)?(?:(?:#{smalls})(?:#{before_ten})|#(?:{smalls})|(?:#{before_twenty}))?#{places[i]}"
end
reg.push out
end
reg.reverse!
reg=reg.map{|t|"(#{t})"}
reg=reg.join "|"
reg.chomp "|"
reg=".*?(#{reg}).*?"
puts reg
x=Regexp.compile(reg)
puts x.match("one")
puts x.match("two")
puts x.match("eleven")
puts x.match("fortyfive")
puts x.match("onehundred")
puts x.match("My number is twohundredthirtyfivethousandsixhundredfortytwo and I am awesome.")
puts x.match("twohundredfortyfivethousandsixhundredtwentyfour")
puts x.match(" twohundredthirtyfivethousandsixhundredfortytwo ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment