Read more

Match a string with wildcards

Tobias Kraze
June 01, 2012Software engineer at makandra GmbH

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/

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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

Posted by Tobias Kraze to makandra dev (2012-06-01 11:31)