Module: Word::Regexp

Included in:
Match
Defined in:
lib/word/regexp.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.has_any_regexp(words, wildcard = Word::WILDCARD, options = true) ⇒ Object



19
20
21
# File 'lib/word/regexp.rb', line 19

def self.has_any_regexp(words, wildcard = Word::WILDCARD, options = true)
  /#{words.map{|w| w.word_regexp(wildcard, options)}.join('|')}/
end

Instance Method Details

#word_regexp(wildcard = Word::WILDCARD, options = true) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/word/regexp.rb', line 5

def word_regexp(wildcard = Word::WILDCARD, options = true)
  wildcard = wildcard || Word::WILDCARD
  prefixed = /^(#{wildcard})/
  suffixed = /(#{wildcard})$/

  prefix = (wildcard and self =~ prefixed) ? "" : "\\b"
  suffix = (wildcard and self =~ suffixed) ? "" : "\\b"

  w = self.gsub(/#{prefixed}|#{suffixed}/, "")
  w = prefix + ::Regexp.escape(w) + suffix

  ::Regexp.new(w, options)
end