Python learning – regular expression

Right now, I am a teaching assistent in University of Oregon (UO) for CIS-122 (Introduction to Programming and Algorithms Using Python). We would like our students to make full use of CScircles of CS department at University of Waterloo (http://cscircles.cemc.uwaterloo.ca/), which is a great online practicing website for Python programming. After quick going thru all these excercises within one week, I decide to post some general solutions and hints for certain topic. For sure, this would be my own ‘Journal’ for studying/teaching this class and hopefully it may provide some help for my students:)

[daveti@aimlab python]$ cat postalValidateReg.py
#!/bin/env python
# Postal code validation code using regex
# for waterloo online CS program – Chapter 14
# 23SEP2012
# daveti

# Define a function postalValidate(S) which first checks if S represents a postal code which is valid:
# first, delete all spaces;
# the remainder must be of the form ‘L#L#L#’ where L are letters (in either lower or upper case) and # are numbers.
# If S is not a valid postal code, return the boolean False.
# If S is valid, return a version of the same postal code in the nice format L#L#L# where each L is capital.

import re

# Pattern ‘L#L#L#’
# L: characters
# #: digits
def postalValidate(S):
    # Need to delete all the spaces within the string
    # strip() only cares about the leading and trailing ones
    postal = S.strip()
    postal2 = ”
    for i in range(len(postal)):
        if postal[ i] != ‘ ‘:
            postal2 += postal[ i]

    postalRegStr = ‘[a-zA-Z]+[0-9]+[a-zA-Z]+[0-9]+[a-zA-Z]+[0-9]+’
    postalRegObj = re.compile(postalRegStr)
    # match() would only try to find the substring matched the pattern
    # regardless if the whole string matches the pattern
    # Need to use span() to find the start and end indexes
    # for the matching to see of the whole string matches…
    # NOTE: span() works when match() finds the matching
    # Otherwise:
    # AttributeError: ‘NoneType’ object has no attribute ‘span’
    matchResult = postalRegObj.match(postal2)
    if matchResult:
        startIdx, endIdx = matchResult.span()
        # NOTE: the endIdx is the last matching index plus 1…
        if startIdx == 0 and endIdx == len(postal2):
            return postal2.upper()
        else:
            return False
    else:
        return False

# Main function
inputStr=input()
print ‘Input: ‘ + inputStr
output=postalValidate(inputStr)
print ‘Output: ‘ + str(output)
[daveti@aimlab python]$

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s