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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.