class Regexp def self.from_wildcard(wildcard, options = '') /^#{wildcard.gsub(/[\.\*\?]/) { |m| { '.' => '\.', '*' => '.*', '?' => '.' }[m] }}$/#{options} end end
Here's the usage and result:
puts Regexp::from_wildcard "?WIN*.EXE" => (?-mix:^.WIN.*\.EXE$) "AWINNER.EXE".match(Regexp::from_wildcard "?win*.exe", 'i') => #(matchdata "AWINNER.EXE")
Have fun! ;)
Online Petition
No comments:
Post a Comment