Regular expressions are my purest bet, in my opinion. If you have not used them before, this one is a good resource. For more information on the Python regex library, go here .
import re regex = re.compile("\x03(?:\d{1,2}(?:,\d{1,2})?)?", re.UNICODE)
The regular expression looks for ^ C (this is \ x03 in ASCII , you can confirm by running chr (3) with the line command), and then optionally look for one or two characters [0-9], then optionally come a comma, and then another or two characters [0-9].
(?: ...) says to forget about keeping what was found in brackets (since we donβt need to do it) ,? means a match of 0 or 1 and {n, m} means a match of n with m of the previous grouping. Finally, \ d means match [0-9].
The rest can be decoded using the links to which I refer above.
>>> regex.sub("", "blabla \x035,12to be colored text and background\x03 blabla") 'blabla to be colored text and background blabla'
Decision
chaos' is similar, but it may end up consuming more than two numbers, and also wonβt remove any free C characters that may be floating around (like the one that closes the color command)
Smerity
source share