An Example: Image Extractor

Let's finish up with an example of a pretty hefty regular expression (two of them, actually), used inside a Perl script. This script takes an HTML file as input, ranges over the file and looks for embedded images (using the <img> tag in HTML). It then prints a list of the images in that page, printing a list of the various attributes of that image (its location, width or height, text alternative, and so on). The output of the script will look something like this:

---------------
Image:  title.gif
   HSPACE: 4
   VPSACE: 4
   ALT: *
---------------
Image: smbullet.gif
   ALT: *
---------------
Image:  rib_bar_wh.gif
   BORDER: 0
   HSPACE: 4
   WIDTH; 50
   HEIGHT: 50
   ALT: --

If you're not familiar with HTML, the <img> tag can be embedded ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.