fix regexp and don't crash on invalid input