0

I am just starting learning regex, and I was wondering if it could be used to parse and style HTML/XHTML code. After reading this hilarious answer I realized that it can't be done. My question is, how do programs and text editors like Dreamweaver, Notepad++, and Sublime text color coordinate their code? It's obviously possible, by some other means, I'm just curious how it is done. My hunch is a long list of key words mixed with some regex. What do you guys think?

Community
  • 1
  • 1
mandelbug
  • 1,548
  • 5
  • 20
  • 32

1 Answers1

2

The essential concept is fairly straightforward:

They parse the code, according to its grammar, and mark different parts of the text as belonging to the appropriate grammar class.

Then, when text is drawn, its color is determined by the attached grammar class.

In practice, syntax highlighters will often use a simplified grammar; leading to a solution that works for most code, but can get confused by uncommon structures. This trade-off is typically worth it.

Williham Totland
  • 28,471
  • 6
  • 52
  • 68