Refactor regexp code

Create a different function for regexp and string comparison.