python中正则表达式的意外结束

时间:2014-11-27 07:45:36

标签: python regex python-2.7

您好我正在学习Web开发,我遇到了这种错误, 这是我的正则表达式代码:

def valid_username(self , username):
  USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
  if USER_RE.match(username):
    return username

def valid_password(self, password):
  PASS_RE = re.compile(r"^.{3,20}$")
  if PASS_RE.match(password) :
    return password

def valid_email(self, email):
  EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S\+$")
  if EMAIL_RE.match(email):
    return email

这是错误日志:

Traceback (most recent call last):
   File "/home/suraj/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
     rv = self.handle_exception(request, response, e)
   File "/home/suraj/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
     rv = self.router.dispatch(request, response)
   File "/home/suraj/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
   File "/home/suraj/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
   return handler.dispatch()
   File "/home/suraj/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
   return self.handle_exception(e, self.app.debug)
   File "/home/suraj/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
   return method(*args, **kwargs)
   File "/home/suraj/Google_Apps/greetings/greetings.py", line 118, in post
   valid_Email = self.valid_email(email)
   File "/home/suraj/Google_Apps/greetings/greetings.py", line 91, in valid_email
   EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S\+$")
   File "/usr/lib/python2.7/re.py", line 190, in compile
   return _compile(pattern, flags)
  `File "/usr/lib/python2.7/re.py", line 244, in _compile
   raise error, v # invalid expression

错误:正则表达式的意外结束

帮助Nedded!提前致谢

1 个答案:

答案 0 :(得分:2)

您未能在此]正则表达式中添加EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S\+$")括号。

re.compile(r"^[\S]+@[\S]+\.[\S]+$")

你不需要角色类。

re.compile(r"^\S+@\S+\.\S+$")

\S+匹配一个或多个非空格字符。