Smith College Computer Science 250, Spring 2013

Pattern Matching Lab

February 13-15, 2013

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

Use a Secure Shell program to Log in to beowulf.csc with your class account You can either use a secure shell to log in to beowulf now and do all your work there, or do your work on a local mac or pc that runs python, and then sftp it later.

Eventually you'll have to sftp or scp your homework file to beowulf and submit it via your class account.

If you want to login now, make sure you use a secure shell whether on PC or Mac.

  • We can use the ssh command to tell it to connect to another machine:
    ssh 250b-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 version 2.7
    . We will use version 2.7 to stay synchronized with the on-line tutorials.
    To start the Python interpreter, just type in the command
    python and hit Enter
    (if you want to use python3 that is fine; the re module works the same; use the print function instead of the print statement)
    You should obtain something like this:

    [jfrankli@beowulf 250s13]$ python
    Python 2.7.3 (default, Apr 30 2012, 21:18:11) 
    [GCC 4.7.0 20120416 (Red Hat 4.7.0-2)] 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 module's 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: