Class: Regexp

Inherits:
Object show all
Defined in:
lib/mini_sanity/util/regexp.rb

Instance Method Summary collapse

Instance Method Details

#match!(str, pos = 0) ⇒ MatchData

Like Regexp#match, but raises an exception if the match fails.

Examples:

/^([^@]+)@(.+)$/.match!("[email protected]")  # === MatchData
/^([^@]+)@(.+)$/.match!("@user")             # raises exception

Parameters:

  • str (String)

    string to search

  • pos (Integer) (defaults to: 0)

    position in str to begin the search

Returns:

  • (MatchData)

Raises:



17
18
19
20
21
22
23
24
25
# File 'lib/mini_sanity/util/regexp.rb', line 17

def match!(str, pos = 0)
  result = self.match(str, pos)
  if result.nil?
    raise MiniSanity::Error.new(nil,
      "String matching #{self.inspect}#{" from position #{pos}" if pos != 0}",
      str.inspect)
  end
  result
end