Generate more robust string regexps in snippets

Fixes #663