Python Tutorial

Tuesday, June 21, 2011

Python regular expression example: Sample url checker

Using regular expression in python:
Let write a sample url checker, each url have three parts separated by dot(.), first part must have www
secound part have at leat one characher between a to z or A to Z or 0 to 9 and third part must have com

import re
def myRegularExpressionChecker(expression,data):
    reg=re.compile(expression);
    if reg.match(data):
        return 'Yes'
    return 'No'

print myRegularExpressionChecker('www[.][a-zA-Z0-9]+[.]com','www.google.com')
print myRegularExpressionChecker('www[.][a-zA-Z0-9]+[.]com','www.cs.edu')
print myRegularExpressionChecker('www[.][a-zA-Z0-9]+[.]com','www.123cseASD.com')
print myRegularExpressionChecker('www[.][a-zA-Z0-9]+[.]com','www.123google.com')
print myRegularExpressionChecker('www[.][a-zA-Z0-9]+[.]com','www.google_123.com')
print myRegularExpressionChecker('www[.][a-zA-Z0-9]+[.]com','www.google.123.com')
Output
 
 Yes
 No
 Yes
 Yes
 No
 No
 
 

1 comments:

markus said...

Good blog. Whether the article on Qt, GTK?
print myRegularExpressionChecker('(|www)((|.)[a-zA-Z0-9]+[.]com)','www.google2123.com') - with www and without www)

Post a Comment