I want to colorize strings in python, inserting start/stop color sequences before/after all numbers, as well as substrings marked with a control character ( % ). Lets assume [ and ] are start/stop color sequence
Currently, I match the string using two substitute iterations:
Numbers: text = re.sub(r'(\d+(\.\d+)?)', '[\\1]', text)
Substrings: text = re.sub(r'%(.*?)%', '[\\1]', text)
Example string: "Test 1.23: Some %string 123 matched%"
Desired output: "Test [1.23]: Some [string 123 matched]"
Actual output: "Test [1.23]: Some [string [123] matched ]"
I've tried (in the number step) to detect if we are already in a colored area without luck, as well as removing all color tags within a %control character sequence%, also without luck.
re.sub..you cannot make them independent