Created
July 27, 2010 16:23
-
-
Save mkroman/492450 to your computer and use it in GitHub Desktop.
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
# 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