1 Replies - 385 Views - Last Post: 24 July 2012 - 06:53 AM Rate Topic: -----

#1 gummz00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-July 12

Error with my RE function

Posted 24 July 2012 - 06:28 AM

Here's my code:

import re
rmf = re_match_function
def re_match_function(input, re):
    re_compilation = re.compile(re)
    re_compilation.match(input)
    a = re_compilation.match(input)
    if a:
        print "Search was successful."
        print "Your match string was: %s." % (a.group())
    else:
        print "Search was not successful. Retry?"
        print "Try again? (Y/N)"
print "re_match_function has been defined, or 'rmf'."


I'm trying to create a function that accepts two arguments: The string to match for in a RE compilation, and a string that tells the function what to compile. When I run this code, this is the error I get when I input, for example "abc", "\D" as arguments into my function:

Quote

AttributeError: 'str' object has no attribute 'compile'


This is weird to me because I can put a string there where "re" is in the function if no function is used. This works fine for example:

re_compilation = re.compile("\d+")
a = re_compilation.search("321")
if a:
    print "Search was successful."
    print "Your match string was: %s." % (a.group())
else:
     print "Search was not successful."
     print "Provide another input and RE compile string to retry."




I've tried googling this to no avail. I've tried putting in arguments without quotation marks, but then this error occurs:

Quote

SyntaxError: unexpected character after line continuation character


Is This A Good Question/Topic? 0
  • +

Replies To: Error with my RE function

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2146
  • View blog
  • Posts: 3,296
  • Joined: 21-June 11

Re: Error with my RE function

Posted 24 July 2012 - 06:53 AM

You called your variable re, so when you do re.compile, it tries to call the method compile on the variable re (which refers to a string and thus does not have a compile method) - not the module re.

Simply call your variable something else and it will work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1