如何正确地放置这些变量?

时间:2013-12-11 05:18:14

标签: python variables refactoring

   u = 'stringandstring'
   b = "network:"
   e = "yeser;"

   def haystack(b,e,u):
         i = re.search('%s(.*)%s', u)
         r = i.group(1)
         return r

  .....
  def haystack(b,e,u):
         i = re.search('b(.*)e', u)
         .....

如何正确地在函数中获取这些变量?

1 个答案:

答案 0 :(得分:1)

我想你可以尝试连接(str1 + str2)

def haystack(b,e,u):
    i = re.search(b+'(.*)'+e, u)
    if i:     #check if there is any result
        return i.group(1)    #return match

#now try to call it
print haystack("this","str","this is str")    #this should output ' is '
print haystack("no","no", "this is str")      #this should not print anything
到目前为止,这对我来说非常合适