ncyoung.com

You are here: Top->Programming->Perl



regular expression for closing image tags

You can use this regular expression to find image tags that are not self closing and make them self closing. It will change:

<img src="spacer.gif" height="1" width="1">

to:

<img src="spacer.gif" height="1" width="1"/>

If you have:

<img src="spacer.gif" height="1" width="1"></img>

It will STILL change it to:

<img src="spacer.gif" height="1" width="1"/></img>

So in that case you can't just replace all.

It should be easy to re-purpose this for other tags that need to be made self closing.

The find term is:

<img ([^>]*)([^/])>

The replace term is:

<img \1\2/>

installing LibXSLT on windows using PPM

I run activeperl on windows. Activeperl's package installer (PPM) usually installs anything I need really easily, but when I went to install LibXSLT (and related libraries) it couldn't do it right off the bat. After a little digging it turned out to be quite easy to do.

First, the default repositories the PPM comes configured to use don't have the libraries in them. From the PPM command line add a new repository using the following command:
rep add xml-stuff http://theoryx5.uwinnipeg.ca/ppms/

Once you've done that the search and install commands you give to PPM will find LibXSLT and LibXML properly.

Depending on what you already have installed, LibXSLT may or may not have everything it needs to run. Sometimes it seems to follow dependencies and other times it just told me what to install next and then quit(!?). In that case use the PPM search and install commands to continue getting the stuff you need.

Some of the libraries depend on dlls. For some reason on my system PPM was unable to put the dlls into the right place (it told me at the time) so I went and got them myself.

Manually putting these three files into my c:/perl/bin directory got me up and running.

http://theoryx5.uwinnipeg.ca/ppms/scripts/libexslt_win32.dll

http://theoryx5.uwinnipeg.ca/ppms/scripts/libxml2.dll

http://theoryx5.uwinnipeg.ca/ppms/scripts/libxslt_win32.dll

matching multi line comments with a regular expression

I was having trouble matching multi-line comments with a regex. The engines I was using tended not to work with the full complement of possible line endings my text files included.

Anyway, the dot together with the "single line" option seemed like it should have worked:

/<!--.*?-->/s

I ended up souping up the dot with a small character range to get it working:

/<!--(.|[\n\r])*?-->/s

Don't forget the ? or you'll get everything from the start of the first comment to the end of the last one.

Oooh and heres a good regex reference

comparison of templating systems

This is a great article comparing web templating systems in perl. It's neat to see how the author has categorized templating systems. Plus I love how he starts the article:


Go on, admit it: you've written a templating system. It's okay, nearly everyone has at some point. You start out with something beautifully simple like $HTML =~ s/$(w+)/${$1}/g and end up adding conditionals and loops and includes until you've created your very own unmaintainable monster.

regex coach

This is a neat tool for debugging or learning regular expressions. It has panes for the string and the expression, and gives feedback in real time as to which parts of the string are matched by the regular expression.

It also has panes for replace expression and the results from that, and it can show how the string will get broken up if the regular expression is used to split the string.

regex coach

perl for drawing ascii tables and charts

There are two neat perl modules for drawing tables and flowcharts. You set up and configure a set of perl objects, which then draw themselves as ascii widgets.

flowcharts

tables