Regex in Python

Discussion in 'Python' started by pradeep, Nov 29, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Well, I was trying to use regular expressions in Python. I wrote a small script, so I thought I'll will post it and make it open for discussion. As I learn more I'll keep them posted too, and if anyone has any comments please do let me know.

    Code:
    #!/usr/bin/python
     
     import re
     
     print "Content-Type: text/html\n\n"
     
     regx = re.compile(r"([A-C])([a-x])?") # Should match either A,B or C, and another character between a-x after tht
     
     sTest = "An Apple A Day Keeps The Doctor Away from Vitamin C"
     
     print "Trying a match..<br/>"
     
     oMatches = regx.match(sTest)
     if not oMatches:
         print "There were no matches"
     else:
         for k, grp in enumerate(oMatches.groups()):
             print "Group %d matched %r<br/>" % (k+1, grp)
     
     
  2. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    it seems interstingly.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice