#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    30
    Rep Power
    0

    Regular Expression


    I created a simple function to check for regular expressions, but it returns with no matches and I can't figure out what is wrong with it. Any help would be great. Thanks

    ---------------------------------------------------
    Code:
    import urllib
    import re
    
    def Content(content):
      required = 'a'
      p = re.compile(required, re.IGNORECASE)
      #print content
      print "\n\nMatch is: \n"
      gr = p.match(content)
      print gr
      print "\n\nGroup is: \n"
      print gr.groups()
    
    s = 'japana'
    Content(s)
    I keep getting "None" matches.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    The problem is that regexObj.match() only matches at the start of the string. What you want to use is regexObj.search(), which will find the pattern anywhere in the string.

    Also, if you use the matchObj.groups() method then you need to define at least one group in you pattern, e.g. '(a)', otherwise it will always return an empty tuple. If you just want to return the matched string then use matchObj.group().

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    30
    Rep Power
    0
    Aaaaah, i should have mentioned that. I already tried group, and I had already put the paranthesis around 'a' as '(a)' but I still get "none" as matches
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    30
    Rep Power
    0
    Figured out the problem when someone told me, that it was looking only for those letters, and not within the words.

IMN logo majestic logo threadwatch logo seochat tools logo