You should not try to parse HTML, XML, JSON, CSV, and other complex grammars using regular expressions, or worse - using manual character/string tokenisation. Instead, use a tried and tested parsing library, which you should be able to find using a web search.