October 10, 2007
Boot the machine in the Linux Operating System.
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.2.2 (#1, Feb 24 2003, 19:13:11) [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] 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+7and 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:
testwill match the string "test" exactly.
. ^ $ * + ? { [ ] \ | ( )
Meta-character Highlights:
>>> print re.match(r'From\s+', 'Fromage amk') NoneWe are searching the string 'Fromage amk' for the expression 'From\s+'. This pattern requires the substring 'From'followed by at least one white space character. (\s matches any whitespace character, and + indicates one or more of them).
>>> m = re.match(r'From\s+', 'From amk Thu May 14 19:12:10 1998') <re.MatchObject instance at 80c5978> >>> m.group() 'From 'This time re.match() returns an object that is not None, and the group() method reveals the substring that matches the pattern.
>>> a = 'i|y' >>> b = 'Judy' >>> c = 'Judi' >>> m = re.search(a,b) >>> m.group() 'y' >>> m = re.search(a,c) >>> m.group() 'i'
>>> m = re.search('(ab)*', 'ab')
>>> m.group()
'ab'
>>> m.group(0)
'ab'
Subgroups are numbered from left to right, from 1 upward. Groups can
be nested; to determine the number, just count the opening
parenthesis characters, going from left to right.
>>> m = re.search('(a(b)c)d', 'abcd')
>>> m.group(0)
'abcd'
>>> m.group(1)
'abc'
>>> m.group(2)
'b'
Backreferences>>> p = r'(\b\w+)\s+\1' >>> m = re.search(p, 'Paris in the the spring') >>> m.group() 'the the'where \b means Word boundary (non alphanumeric) and \w means word (alphanumeric character, i.e. equivalent to the class [a-zA-Z0-9_].) Backreferences like this aren't often useful for just searching through a string -- there are few text formats which repeat data in this way -- but you'll soon find out that they're very useful when performing string substitutions. Why wouldn't this pattern work on 'Paris in thethe spring' ?
emacs lab250.pyand in emacs type in the following.:
import re # import the reg expr module functions
def main(): # define function called main
a = "I like Red Hat Linux for development."
m = re.search(r"(.{7})Linux", a) # Any 7 characters then string 'Linux'
b = m.group(1) # The first group in parentheses
print "<" + b + ">"
print m.group(0) # The whole match
Save and exit emacs then invoke python to
call the function:
jfrankli-PB1:~ jfrankli$ python Python 2.3.5 (#1, Mar 20 2005, 20:38:20) [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import lab250 >>> lab250.main() <ed Hat > ed Hat Linux >>>You can also import and run re functions in the python interpreter:
jfrankli-PB1:~ jfrankli$ python
Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re # import the reg expr module functions
>>> a = "I like Red Hat Linux for development."
>>> m = re.search(r"(.{7})Linux", a)
>>> b = m.group(1)
>>> print "<" + b + ">"
<ed Hat >
>>> print m.group(0)
'ed Hat Linux'
Now consider the examples from the free version of RedHat Linux6Unleashed
http://freebooks.by.ru/view/RedHatLinux6Unleashed/rhl6u350.htm.
Start half way down with the discussion on Regular Expressions.
We'll go through this together.
Now go through this example again and see if you remember what it does.
>>> a = r"Valentines is 2/14/2000. Don\'t forget!"
>>> import re
>>> m = re.search(r"\D\d{1,2}/\d{1,2}/\d{2,4}\D", a)
>>> m.group(0)
How about this one?
>>> s = "My, how are you"
>>> m = re.search("^how are you$", s)
>>>
Why didn't this match? What can you do to make it match,
including the question mark?
import re
def simul():
a = "11/21/1999"
m = re.search(r"^([01]?\d)[/-]([0123]?\d)[/-](\d{2,4})", a)
month,day,year = m.groups()
print year, month, day
Make sure you understand this before moving on.beowulf$ getcopy test.tstor download test.tst here. Then in emacs in your file lab250.py, add this function definition:
def brack():
infile = open("test.tst", "r")
x = infile.readline() # read one line from file
while x != "":
x = x[:-1] # slice off newline char at end of string
m = re.search(r"\[\s*(.*)\s*\]", x) # treat bracket as just bracket char
if m:
print m.group(1) # just print inbside group
x = infile.readline()
infile.close()
Figure out why the lines that matched did by
examining test.tst.
def email(addr):
b = r"^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"
m = re.search(b, addr, re.IGNORECASE) # allow upper or lower case
print m.group(0)
\b means word boundary (anything that is not alphanumeric or
underscore qualifies).>>> a = r'<html\b[^>]*>(.*?)</html>' >>> m = re.search(a,'<html><H1>Test Page</H1><p>This is just a test</p></html>') >>> m <_sre.SRE_Match object at 0x96860> >>> m.group() '<html><H1>Test Page</H1><p>This is just a test</p></html>' >>> m.group(0) '<html><H1>Test Page</H1><p>This is just a test</p></html>' >>> m.group(1) '<H1>Test Page</H1><p>This is just a test</p>' >>> m.group(2) Traceback (most recent call last): File "Using backreference:", line 1, in ? IndexError: no such group
>>> a = r'<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>'
>>> a
'<([A-Z][A-Z0-9]*)\\b[^>]*>(.*?)</\\1>'
>>> b = '<html><H1>Test Page</H1><p>This is just a test</p></html>'
>>> b
'<html><H1>Test Page</H1><p>This is just a test</p></html>'
>>> m = re.search(a,b,re.IGNORECASE)
>>> m
<_sre.SRE_Match object at 0x741d0>
>>> m.groups()
('html', '<H1>Test Page</H1><p>This is just a test</p>')
>>>
Don't forget to logout.