Make strings regex-safe before using them.

Fixes StringIndexOutOfBoundsException.