Use regex for whitespace checking instead of for loop