Match a string with wildcards

Posted . Visible to the public.

The following snippet will convert a string with wildcards to a appropriate regexp, i.e.
parse_wildcards("foobar") # => /\Afoo.bar\z/
parse_wildcards("
") # => /\A.
\z/
parse_wildcards("[") # => /\A[\z/

^
def parse_wildcards(string)
matching_parts = string.split('', -1).collect { |part| Regexp.escape(part) }
/\A#{matching_parts.join(".
")}\z/
end

Tobias Kraze
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Tobias Kraze to makandra dev (2012-06-01 09:31)