Regular Expressions, and what I don’t know
Published September 26th, 2005 in websiteSo wow, I learned quite easily today while I was working on my syntax highlighter that I need to learn how to use regular expressions quite a bit better. In fact I would say I just need to learn them. I was able to piece together what I needed to do, but it probably should have only taken me 20 minutes at most to do what I did and instead that took about an hour.
So anyways, basically what I did was to go in to the section of Textile1 where it handles the code tags in the textile function and added my own code. One early problem I noticed was that I couldn’t figure out how to just grab a language field out of the code tag, i.e. code lang=”vb” so instead, and for the sake of time I just decided to make my own tags, in this case one for each language: vb, csharp and just put my code in there.
So basically I just start inside the for loop that goes through your post and changed the first line to:
if(preg_match( ’ /< (code|vb|csharp|asp)>/i’,$line)){$codepre = true; }
if(preg_match( ’ /< \/(code|vb|csharp|asp)>/i’,$line)) { $codepre = false; }
After that, I set various flags for the different languages so that I know what language has been chosen. Again, I’ll say this, it’s not the prettiest code, but it works for now and is a good start in my opinion for my first forray into regular expressions.
if(preg_match(’/[vb]/i’, $line)) { $vbpre = true };
Now I turn all those tags, vb, csharp, etc.. into pre tags so that on the page they all have the same style. The yellow background and a monospaced font. I chose a few fonts personally, the first being Bitstream Vera Sans Mono because that is what I use more often than not, then Andale, Courier, and Monospace just to make sure it will at least be monospaced no matter where you view it, whether you have the font I use or not. Of course all you good web designers know that so anyways. From there I call a coloring function on the line depending on the chosen language.
if($cspre == true) { $line = color_cs($line); }
That particular function looks something like this:
function color_cs($text) {
$keywords = array(’/private/’, ’/public/’,’/protected/’);
$text = preg_replace($keywords, “<span style=\"color:#00f\"></span>”, $text);
$text = preg_replace(”/\/\/.*\n/”, “<span style=\"color:#0a0\"></span>”, $text);
return $text;
}
The only other thing to do is to take into account that I changed those vb, csharp, etc.. tags into pre tags and now need to close those off, so at the very end of the textile function.
So now how to make it better? Well for one, getting that to work as code lang=”language” because the code tag is rendered so I could use it alot like a pre tag and not have to worry about the conversions. On top of that making it read those regular expressions from an xml file to make it easily updatable. There are a few other little quirks in there as well, but for now it makes it much easier to read.
No Responses to “Regular Expressions, and what I don’t know”
Please Wait
Leave a Reply