Move regex compilation out of the loop