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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)