Skip to content

Instantly share code, notes, and snippets.

@mkroman
Created July 27, 2010 16:23
Show Gist options
  • Save mkroman/492450 to your computer and use it in GitHub Desktop.
Save mkroman/492450 to your computer and use it in GitHub Desktop.
# encoding: utf-8
module Wildcard
def self.match pattern, source, casefold = false
pattern = compile pattern, casefold
!(source =~ pattern).nil?
end
protected
def self.compile pattern, casefold
['.', '[', ']', '(', ')'].each do |reserved|
pattern.gsub! reserved, '\\' + reserved
end
pattern.gsub! '?', '.'
pattern.gsub! '*', '.*?'
if casefold
Regexp.compile pattern, Regexp::IGNORECASE
else
Regexp.compile pattern
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment