method

match

rspec latest stable - Class: Spec::Matchers
match(expected)
public

Given a Regexp or String, passes if actual.match(pattern)

Examples

  email.should match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
  email.should match("@example.com")

2Notes

Careful when comparing strings

emime · Apr 22, 2011

Input String is treated as Regexp:

"*test*".should match "*test*" #=> fail
"*test*".should match ".*test.*" #=> pass

Regexp special characters inside input String can't [be] escaped:

"test".should match "\test\" #=> fail "test".should match /\test\/ #=> pass

Input strings are treated as regexp

emime · Apr 24, 2011

Input strings are treated as regexp, but you can escape special regexp characters as usual:

"test".should match "\\test\\" #=> pass "test".should match '\test\' #=> pass "test".should match /\test\/ #=> pass