I have a string that contains colours in the form of “^##” where ## can be 00-99.
I wrote regex to detect and replace these colours:
Input = Regex.Replace(Input, "\^[0-9][0-9]", "");
However the compiler doesn’t seem to like \^ as a means of detecting the “^” character (gives an invalid escape code error). So how do I go about looking for the ^ character in c# regex?
That happens because, well, there’s no such escape sequence (
\^)You can use:
@"\^[0-9][0-9]""\\^[0-9][0-9]"Tips:
[0-9]is equivalent to the shorthand\d[0-9][0-9]you could use[0-9]{2}(or\d{2}). This helps when you have more repetitions.References:
Character classes, Repetition