O'Reilly logo

Python 2.6 Text Processing Beginner's Guide by Jeff McNeil

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - testing an HTTP URL

In this example, we'll check values as they're entered via the command line as a means to introduce the technology. We'll dive deeper into regular expressions as we move forward. We'll be scanning URLs to ensure our end users inputted valid data.

  1. Create a new file and name it number_regex.py.
  2. Enter the following code:
    import sys
    import re
    # Make sure we have a single URL argument.
    if len(sys.argv) != 2:
    	print >>sys.stderr, "URL Required"
    	sys.exit(-1)
    # Easier access.
    url = sys.argv[1]
    # Ensure we were passed a somewhat valid URL.
    # This is a superficial test.
    if re.match(r'^https?:/{2}\w.+$', url):
    	print "This looks valid"
    else:
    	print "This looks invalid"
    
  3. Now, run the example script on the command line a few ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required