You have a problem. You use regular expressions. You now have solved your problem with a tool used by thousands and thousands of engineers for decades.
Regex anxiety is a red flag - like a chef that is afraid of knives
a Schelling point for those who seek one