Regular expressions
Sunday, February 23rd, 2003Warning! Nerd-speak follows…
I don’t know if I’m the only software developer doing it this way but I like to use regular expressions to handle my syntax hilighting. The trick is to get it to return a style code when a match of a certain part of a string is found.
The following regexp defines the format of a CSS document for the benefit of .
\:im:
/`*[.\n]+?(\Z|`*/)\=2f
|\=?0({\=:1\=e|\w+\=47|.\=2)
|\=?1(}\=:0\=e|:\=:2\=40|[\w\-]+\=1)
|\=?2(}\=:0\=e|;\=:1\=1|.\=7)
This incredibly ugly but fairly compact expression tells JujuEdit to render CSS text thusly:
/*====================== JUJUSOFT STYLE SHEET ======================*/ body { margin: 0; background-color: #fff; font-size: 70%; font-family: Verdana, Tahoma, sans-serif; } blockquote { margin-top:1em; margin-left: 3em; }
I’m fairly pleased with this particular example, since it demonstrates my first use of a modal regular expression, ie, the expression is like a program which can set a state variable and test that variable later on. This helps it keep track of whether it is currently inside or outside of an element definition, and could even be modified to help flag user errors. It brings a regexp closer to being able to define a grammar [although in the long run it would probably make more sense to use a grammar parser to define a grammar]
Leave a Comment