Smith College Computer Science 250b, Spring 2010

Pattern Matching Lab

February 8, 2010

Work with a partner in class. I also encourage you to work with a partner on the homework

Boot in the Mac OSX Operating System.


Log in using your class account (we are going to use 111b accounts, coincidently being taught by Judy as well).
Eventually you'll have to sftp or scp your homework file to beowulf and submit it via your class account.
You can either ssh to beowulf now and do all your work there, or do your work on the local mac, which runs python, and then sftp it later.

If you want to login now,

  • We can use the ssh command to tell it to connect to another machine:
    ssh 111b-xx@beowulf.csc.smith.edu
    
    where xx are the two letters in your own class account username. ssh stands for (Secure Shell) Client.
    When you are prompted, type in the password that you received with the account name. You will see some messages and finally you will see beowulf's prompt -- a short line of text ending with $.

    run the Python Interpreter
    To start the Python interpreter, just type in the command
    python and hit Enter
    You should obtain something like this:

    Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51) 
    [GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    
    where >>> is the Python interpreter's prompt. The prompt is prompting you for a Python command, or statement. Try this:
    >>> print "Hello, World!"
    and also this:
    >>> print 5+7
    and this:
    >>> print 16+32, "is the sum of 16 and 32"
    Next, check out how easy string concatenation is in python with the plus (+) operator (+ is not or in python, as it is in our 250 textbook):
    >>> x = "computer"
    >>> y = "science"
    >>> z = x + " " + science
    >>> print z
    

    Exit from the Python Intepreter

    Exit from the Python interpreter by typing Ctrl-D.

    Python's re module:
    Regular expressions (or REs) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the re module. Using this little language, you specify the rules for the set of possible strings that you want to match; this set might contain English sentences, or e-mail addresses, or TeX commands, or anything you like. You can then ask questions such as ``Does this string match the pattern?'', or ``Is there a match for the pattern anywhere in this string?''. You can also use REs to modify a string or to split it apart in various ways. Most letters and characters will simply match themselves. For example, the regular expression

    test
    
    will match the string "test" exactly.
    Meta-Characters: characters that are part of the re alphabet, but don't match themselves. Instead, they have special meaning to the re interpreter. The * is very much like the * we have seen in our theoretical regular expressions. In practice the re module allows other meta-characters. They are:
    . ^ $ * + ? { [ ] \ | ( )
    
    Meta-character Highlights: