I have never studied how other forums and other programs enable syntax highlighting. Some may use regular expressions. Regular Expressions are easy to understand. Different regular expressions are required for keywords, comments, and strings, each word is matched using the regular expression of the keyword, and the full text is annotated and matched with the string pattern.
Here I will introduce another method to achieve syntax highlighting. in the Visual Studio SDK, a tool is provided: MPLex and MPPG. Visual Studio integrates the new programming language. The newly integrated programming language can use these two tools to implement syntax highlighting, smart drop-down boxes, and other functions. If you have experience using Lex and Yacc, MPLex is actually the C # version of Lex, and MPPG is the C # version corresponding to Yacc. The former is a lexical processing tool, and the latter is a syntax processing tool.
We only use MPLex, because syntax highlighting only involves lexical processing and does not need to consider syntax issues-of course, if you want to achieve the effect that can be highlighted like Visual Studio, syntax processing and semantic processing can be considered.
The principle is too late to be written today. If you are interested, please download the following code for compilation and execution. You can directly tap C # code in it. Because the algorithm used to process real-time input is to process only one line of text at a time (in most cases), there is a Bug when processing real-time multi-line Annotations: Click to download the source code