Do you need a loop here? Can you just generate the regex for matching all tags once?