[英]Understanding re.search in Python
I am trying to allow users to Enter alphanumerics,underscores and spaces for a name. 我正在尝试允许用户输入字母数字,下划线和空格作为名称。 I've been using
if not re.search(r'^[\\w-]+$', name):
to check if the name has anything other than alphanumerics,underscores and spaces. 我一直在使用
if not re.search(r'^[\\w-]+$', name):
检查名称是否包含字母数字,下划线和空格。 But here I can just enter one word, not multiple. 但是在这里我只能输入一个单词,不能输入多个单词。 What am I doing wrong?
我究竟做错了什么?
You're not doing anything obviously wrong. 您没有做任何明显错误的事情。 Your question isn't totally clear either, so I'll try to address a couple of the possible issues.
您的问题也不是很清楚,所以我将尝试解决几个可能的问题。
In your regex, you have \\w
, which according to the re
documentation , is just alphanumeric characters and underscore. 在正则表达式中,您有
\\w
,根据re
文档 ,它只是字母数字字符和下划线。 What you have not included is the space character, so any name with a space (including if it's separating first and last name) will be matched by your regular expression, and therefore turned to False
by the not
. 您尚未包含的是空格字符,因此带空格的任何名称(包括姓和名之间的分隔符)都将与您的正则表达式匹配,因此由
not
变为False
。 What you should do to check if there are any non-alphanumeric characters/underscores/spaces is the following: 您应该执行以下操作来检查是否存在任何非字母数字字符/下划线/空格:
if not re.search(r'^[\w- ]+$', name):
Or, if you also for some reason are concerned about newlines ( \\n
) or tabs ( \\t
) and other kinds of whitespace: 或者,如果您由于某种原因也担心换行符(
\\n
)或制表符( \\t
)和其他类型的空白:
if not re.search(r'^[\w\s- ]+$', name):
That's because the regex will only search one string at a time. 这是因为正则表达式一次只能搜索一个字符串。 If you want to have it check multiple names, you should run the regular expression against each name, in a format like this:
如果要让它检查多个名称,则应针对每个名称运行正则表达式,格式如下:
# assuming name_list is a list of name strings
for name in name_list:
if not re.search(r'^[\w- ]+$', name):
# do whatever
Your regex doesn't allow spaces (instead it allows dashes which you didn't mention in your question). 您的正则表达式不允许使用空格(而是允许您在问题中未提及的破折号)。
if not re.search(r'^[\w ]+$', name):
only allows ASCII letters, digits, underscore and spaces (and requires at least one character). 仅允许ASCII字母,数字,下划线和空格(并且至少需要一个字符)。
You need to learn a little bit about regular expressions. 您需要学习一些有关正则表达式的知识。
I personally started from Google classes and it was good enough to read the docs . 我个人是从Google课程开始的,足以阅读这些文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.