|
|
|
|
Using Perl and Regular Expressions to Process Html Files – Part 2Author: John Dixon In this article we will discuss how to change the contents of an HTML file by running a Perl script on it. The file we are going to process is called file1.htm: Note: To ensure that the code is displayed correctly, in the example code shown in this article, square brackets ‘[..]‘ are used in HTML tags instead of angle brackets ”. [html] Imagine that we need to change both occurrences of [h1]heading[/h1] to [h1 class="big"]heading[/h1]. Not a big change and something that could be easily done manually or by doing a simple search and replace. But we’re just getting started here. To do this, we could use the following Perl script (script1.pl): 1 open (IN, “file1.htm”); Note: You don’t need to enter the line numbers. I’ve included them simply so that I can reference individual lines in the script. Let’s look at each line of the script. Line 1 Line 2 Line 3 Line 4 Looking at Line 4 in more detail:
Line 5 Line 6 Lines 7 and 8 Running the Script As the purpose of this article is to explain how to use regular expressions to process HTML files, and not necessarily how to use Perl, I don’t want to spend too long describing how to run Perl scripts. Suffice to say that you can run them in various ways, for example, from within a text editor such as TextPad, by double-clicking the perl script (script1.pl), or by running the script from an MS-DOS window. (The location of the Perl interpreter will need to be in your PATH statement so that you can run Perl scripts from any location on your computer and not just from within the directory where the interpreter (perl.exe) itself is installed.) So, to run our script we could open an MS-DOS window and navigate to the location where the script and the HTML file are located. To keep life simple I’ve assumed that these two files are in the same folder (or directory). The command to run the script is: C:>perl script1.pl If the script does work (and hopefully it will), a new file (new_file1.htm) is created in the same folder as file1.htm. If you open the file you’ll see the the two lines that contained [h1] tags have been modified so that they now read [h1 class="big"]. In Part 3 we’ll look at how to handle multiple files. Article Source: http://www.articlesbase.com/programming-articles/using-perl-and-regular-expressions-to-process-html-files-part-2-362029.html About the Author John is a web developer working for My Health Questions Matter, a company dedicated to helping patients to get the most out of their interaction with health care professionals such as doctors, midwives, and consultants by generating a set of health questions a patient can ask at an appointment. Related Posts Comments2 Responses to “Using Perl and Regular Expressions to Process Html Files – Part 2” |
[...] Perl, and PHP are all interpreted programming languages.[/p] [/body] [/html] View post: Using Perl and Regular Expressions to Process Html Files – Part 2 … Share and [...]
Wiersz Polece? Windows CMD…
Windows XP Vosten SP3 PRO 2010 released now! this windows combining the beauty of appearance and the advanced performance, The System has been updated completely with the latest updates and hotfixes from Microsoft to maintain the highest level of secur…