Auto AdSense

Saturday, 24 January 2015

Python Programs - Regular Expressions - Search Function






  •       #!/usr/bin/python
    import re

    line = "Cats are smarter than dogs";

    matchObj = re.search( r'(.*) are(\.*)', line, re.M|re.I)

    if matchObj:
       print "matchObj.group() : ", matchObj.group()
       print "matchObj.group(1) : ", matchObj.group(1)
       print "matchObj.group(2) : ", matchObj.group(2)
    else:
       print "No match!!"
  • Output
    matchObj.group(): Cats are
    matchObj.group(1) : Cats
    matchObj.group(2) :

No comments:

Post a Comment