Regular Expressions

pix
Go back to home page

This page provides an example of using regular expressions in Python to do some automated text processing. The task here requires the processed text to be emedded in some simple HTML tags, and displayed on the web. Thus this task also provides an introduction to the CGI process.

This page is obsolete.

The latest page is at itmetr.net.

last modified: 05:04 PM MDT, Sun 15 Jun 2008

Some CGI basics

If you dabbled with my html examples within htmlex.tar, available from my HTML Tutorial, then you may already have a working CGI script at your website. You may have one like mine: mesograb.cgi.

In order to learn the essence of CGI, you are invited to play with that script. Here are some simple experiments you can do.

  1. From your Linux command line, execute the script mesograb.cgi, just as you would any other python script. Notice you see the HTML tags that you do not see directly when the output is viewed through your browser.

  2. Notice that mesograb.cgi accepts a command line argument, for example:
    	mesograb.cgi ALTU
    	

  3. mesograb.cgi also accepts arguments when executed through a browser: just join things with a "?" rather than " ": mesograb.cgi?ALTU.

  4. The first line that the CGI script prints should be
    Content-type: text/html
    followed by a blank line. I have read that the client computer demands this line. This seem reasonable to me. Yet when I send a text file or html file over the web, I do not need to put that line in my file, and the client side stays happy. So perhaps the server (Gentry) normally prepends the Content-type line to files from my website.

  5. Can we claim CGI is easy? Usually so, but sometimes not. There is sometimes a big chasm to cross between getting a Python script to function for a Linux command line, with permissions set for the owner of the program, and getting Apache to run the same program at the request of a remote user. Hopefully that chasm will be effortlessly crossed in the CGI task for this course.

Links about Elementary CGI with Python


Regular Expressions

Regular expressions are essential to almost every programmer and every workstation user. There is good information about regular expressions on the web; links are provided below. Your textbook Practical Python also has a decent section on the regular expressions. So this "tutorial" consist of some simple example programs that use regular expressions.

General Information about Regular Expressions:

python regular expressions:

vim regular expressions, for those who might need it:


Previous tasks now offered as examples

The following "examples" are scripts that were offered for previous years as a starting point for students' tasks. The students expanded on these scripts, but those expansions are not posted here (yet).


The task for Spring 2006 is reformatting NWS text products. This is a python script that reformats ugly NWS text products.

this is an obsolete site
go to new site
go to obsolete home page

pix
Move to top of page