On Wed, 19 Dec 2001, Gilles Filippini wrote:

> Hello,
> 
> There must be a way to check if a string matches a file pattern.
> But I can't figure it out.
> Any help appreciated.

Hello Gilles,

I'm not sure what you mean, but if you try to check things like

  "filename.txt".matches? "*.txt"   #=> true
  "foo.bor".matches? "f?o.b?r"      #=> true

you can code something like

  class String
    def matches?(pattern)
      pattern = pattern.gsub(/\./, '\.')
      pattern = pattern.gsub(/\?/, ".")
      pattern = pattern.gsub(/\*/, ".*?")
      re = Regexp.new(pattern)
      re.match(self) != nil
    end
  end

  p "filename.txt".matches? "*.txt"
  p "foo.bor".matches? "f?o.b?r"

     - Aleksi