Don't let regex extend outside of string bounds

This corrects an issue with legal strings such as `r'('` and `r'[[]'`. Fixes #1.