The KM Search and Replace action using RegEx should be able to do what you need.
But you will need to post a real-world example of your source text, and of the desired resultant text, using the Forum Code Block.
Please make sure you provide examples for all of your use cases.
Please read:
Tip: How Do I Get The Best Answer in the Shortest Time?