I tried with a regex but it was becoming a lot harder to understand :(