Like quite a few world-wide-web content authors, around the earlier number of many years I’ve experienced quite a few situations when I’ve necessary to clear up a bunch of HTML files that have been produced by a word processor or publishing package deal. Initially, I made use of to clear up the files manually, opening just about every just one in flip, and earning the exact same established of updates to just about every just one. This performs great when you only have a number of files to take care of, but when you have hundreds or even hundreds to do, you can pretty rapidly be looking at weeks or even months of perform. A number of many years ago somebody place me on to the thought of applying Perl and regular expressions to carry out this ‘cleaning up’ course of action.
Why create an write-up about Perl and regular expressions I listen to you say. Perfectly, which is a excellent issue. Right after all the world-wide-web is complete of tutorials on Perl and regular expressions. What I uncovered while, was that when I was trying to discover out how I could course of action HTML files, I uncovered it tricky to discover tutorials that achieved my criteria. I’m not stating they don’t exist, I just couldn’t discover them. Positive, I could discover tutorials that discussed all the things I necessary to know about regular expressions, and I could discover a good deal of tutorials about how to software in Perl, and even how to use regular expressions inside of Perl scripts. What I couldn’t discover while, was a tutorial that discussed how to open just one or much more HTML or text files, make updates to these files applying regular expressions, and then help you save and shut the files.
When converting files into HTML the intention is often to achieve a seamless conversion from the supply doc (for illustration, a word processor doc) to HTML. The last issue you need to have is for your content authors to be paying out several hours, or even days, fixing untidy HTML code immediately after it has been converted.
Numerous apps offer great tools for converting files to HTML and, in blend with a well developed cascading fashion sheet (CSS), can often deliver great results. In some cases while, there are tiny bits of HTML code that are a little bit messy, generally brought about by authors not applying paragraph tags or models effectively in the supply doc.
The reason why Perl is this sort of a excellent language to use for this undertaking is since it is great at processing text files, which let us facial area it, is all HTML files are. Perl is also the de facto standard for the use of regular expressions, which you can use to look for for, and substitute/improve, bits of text or code in a file.
What is Perl?
Perl (Practical Extraction and Report Language) is a general intent programming language, which means it can be made use of to do anything that any other programming language can do. Possessing claimed that, Perl is pretty excellent at accomplishing specified matters, and not so excellent at some others. Though you could do it, you wouldn’t generally create a consumer interface in Perl as it would be much less difficult to use a language like Visual Basic to do this. What Perl is really excellent at, is processing text. This makes it a terrific preference for manipulating HTML files.
What is a Frequent Expression?
In part two, we are going to glimpse at our first illustration Perl script