Monday, October 3, 2011

Python Regular Expression

#!/usr/bin/python
#Author: Balasubramaniam Natarajan
#Demonstrate how to use Regular Expression
import re

def search(searchstr):
    print "*********************************"
    print "searchstr = ", searchstr
    print "*********************************"

searchstr = raw_input("Enter a searching string : ")
search(searchstr)

re1 = re.compile('\w+\s\w+\s\w+\s\d',re.IGNORECASE)
match1 = re1.match(searchstr)

if match1:
   print "Matching \w+\s\w+\s\w+\s\d : ",match1.group()
else:
   print "No match"

#END

Output

bala@bala-laptop:~/python$ python RE.py
Enter a searching string : Apples cost RS 20
*********************************
searchstr =  Apples cost RS 20
*********************************
Matching \w+\s\w+\s\w+\s\d :  Apples cost RS 2
bala@bala-laptop:~/python$

No comments:

Post a Comment