使用正则表达式来确保字符串是字母数字加号的 Python 代码。- _

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2519670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:49:58  来源:igfitidea点击:

Python code to use a regular expression to make sure a string is alphanumeric plus . - _

pythonregexalphanumeric

提问by Warlax

I looked and searched and couldn't find what I needed although I think it should be simple (if you have any Python experience, which I don't).

我查看并搜索并找不到我需要的东西,尽管我认为它应该很简单(如果您有任何 Python 经验,我没有)。

Given a string, I want to verify, in Python, that it contains ONLY alphanumeric characters: a-zA-Z0-9and ._-

给定一个字符串,我想在 Python 中验证它是否只包含字母数字字符:a-zA-Z0-9._-

examples:

例子:

Accepted:

公认:

bill-gates

bill-gates

Steve_Jobs

Steve_Jobs

Micro.soft

Micro.soft

Rejected:

拒绝:

Bill gates-- no spaces allowed

Bill gates-- 不允许有空格

[email protected]-- @ is not alphanumeric

[email protected]-- @ 不是字母数字

I'm trying to use:

我正在尝试使用:

if re.match("^[a-zA-Z0-9_.-]+$", username) == True:

if re.match("^[a-zA-Z0-9_.-]+$", username) == True:

But that doesn't seem to do the job...

但这似乎并没有完成这项工作......

回答by Mark Rushakoff

re.matchdoes not return a boolean; it returns a MatchObjecton a match, or Noneon a non-match.

re.match不返回布尔值;它MatchObject在匹配或None不匹配时返回 a 。

>>> re.match("^[a-zA-Z0-9_.-]+$", "hello")
<_sre.SRE_Match object at 0xb7600250>
>>> re.match("^[a-zA-Z0-9_.-]+$", "    ")
>>> print re.match("^[a-zA-Z0-9_.-]+$", "    ")
None

So, you shouldn't do re.match(...) == True; rather, you should be checking re.match(...) is not Nonein this case, which can be further shortened to just if re.match(...).

所以,你不应该这样做re.match(...) == True;相反,您应该re.match(...) is not None在这种情况下进行检查,可以进一步缩短为 just if re.match(...)

回答by Ignacio Vazquez-Abrams

Never use == Trueor == Falsein a comparison. Many types already have a bool equivalent which you should use instead:

切勿使用== True== False进行比较。许多类型已经有一个 bool 等效项,您应该改用它:

if re.match("^[a-zA-Z0-9_.-]+$", username):

回答by Ignacio Vazquez-Abrams

Could also shorten it slightly to :

也可以稍微缩短为:

if re.match(r'^[\w.-]+$', username):

回答by sp_omer

I would consider this for a valid username:
1) Username must be 6-30 characters long
2) Username may only contain:

我会认为这是一个有效的用户名:
1) 用户名​​必须是 6-30 个字符长
2) 用户名​​只能包含:

  • Uppercase and lowercase letters
  • Numbers from 0-9 and
  • Special characters _ - .
  • 大写和小写字母
  • 0-9 的数字和
  • 特殊字符 _ - 。

3) Username may not:

3) 用户名​​不得:

  • Begin or finish with characters _ - .

  • Have more than one sequential character _ - . inside

  • 以字符 _ - 开始或结束。

  • 有多个连续字符 _ - 。里面

This would be example of usage:
if re.match(r'^(?![-._])(?!.*[_.-]{2})[\w.-]{6,30}(?<![-._])$',username) is not None:

这将是使用示例:
if re.match(r'^(?![-._])(?!.*[_.-]{2})[\w.-]{6,30}(?<![-._])$',username) is not None:

回答by fabrizioM

If you are going to use many regular expressions you can compile it for speed (or readability)

如果您打算使用许多正则表达式,您可以对其进行编译以提高速度(或可读性)

import re 
ALPHANUM=re.compile('^[a-zA-Z0-9_.-]+$')

for u in users:
    if ALPHANUM.match(u) is None:
        print "invalid"

From the docs:

文档

The compiled versions of the most recent patterns passed to re.match(), re.search()or re.compile()are cached, so programs that use only a few regular expressions at a time needn't worry about compiling regular expressions.

最近模式的编译版本传递给re.match()re.search()或者re.compile()被缓存起来,因此只有在一个时间不用担心编译正则表达式使用一些正则表达式的程序。

回答by Daniel Watson

I do my validation this way in my utils class:

我在我的 utils 类中以这种方式进行验证:

def valid_re(self, s, r):
 reg = re.compile(r)
 return reg.match(s)

Then I call the utils instance, and check this way:

然后我调用 utils 实例,并以这种方式检查:

if not utils.valid_re(username, r'^[a-zA-Z0-9_.-]+$'):
        error = "Invalid username!"