Improve regex pattern and remove trim related code