/regexp_text_numbers_evil.rb Secret
Created
July 27, 2015 06:00
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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