Case insensitive and pattern matching

This replaces pull request #1751. This is for issue #1724.
Is there need to support non-string patterns? Wouldn't it be better to just to

  raise ValueError("Pattern must be string, got '%s'." % type(pattern).__name__)