You need to divide the text into lexical tokens (words, numbers, symbols, and so on), find out what each one is, and colour it accordingly. It's easy enough to recognize numbers and symbols, but to know whether a word is a variable, a function, a keyword or whatever means parsing the text according to the syntactical rules of the language. That's why your search finds references to lexical analysis (Lex) and parsing (Yacc). Lexical analysis is about assembling letters and symbols into words and other tokens, and parsing is about how those tokens go together to make up a syntactically valid program.
Python has a library module, tokenize, that does exactly what you need for the Python language. The documentation even says that it is useful for pretty-printing and colouring on-screen displays. Hopefully, using that will give you more of an idea how all this stuff works. Then you can either search for Python libraries for parsing other languages, or have a go at writing one yourself.
There's a Stack Overflow question here that suggests pyPEG for parsing other languages. Jimothy's suggests of Pygments is good too.