在 python 中对字符串应用过滤器
我有一个用户输入他的用户名,我只想传递有效的字符串,这意味着仅 [a-zA-Z0-9] 中的字符。我对 python 很陌生,不确定语法。
这是我想要的代码示例,即检查用户名并在非法字符时返回 false。:
def _checkInput(input):
for char in input:
if !(char in [a-zA-Z0-9]):
return False
return True
谢谢!
I have a user typing in his username and I only want valid strings to pass through, meaning only characters in [a-zA-Z0-9]. I am pretty new to python and unsure of the syntax.
Here's an example of what I want in code, which is to check through the username and return false upon a illegal character.:
def _checkInput(input):
for char in input:
if !(char in [a-zA-Z0-9]):
return False
return True
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
字符串中有一个名为
isalnum
的方法。它会实现您想要实现的目标。There is a method in string called
isalnum
. It does what you are trying to achieve.您需要 isalnum:
You need isalnum:
Python 字符串有很多有用的方法来执行此类检查,例如:
str.isalnum()
str.isalpha()
str.isdigit()
str.islower()
str.istitle()
str.isupper()
你需要的是
str.isalnum()
如果字符串中的所有字符都是字母数字并且至少有一个字符,则返回 true。如上面的示例所示,字母和数字被视为字母数字,但空格和标点符号则不然。
另请注意,与纯数学相反,空字符串不被视为字母数字。然而,在大多数情况下,这实际上是您所需要的,并且肯定是您所需要的,因为长度为零的用户名没有多大意义。
Python strings have lots of useful methods for doing this sort of check, such as:
str.isalnum()
str.isalpha()
str.isdigit()
str.islower()
str.istitle()
str.isupper()
What you need is
str.isalnum()
which returns true if all characters in the string are alphanumeric and there is at least one character.As the example above shows, letters and numbers are considered alphanumeric, but spaces and punctuation marks are not.
Also note that contrary to what would be mathematically pure, the empty string is not considered alphanumeric. However in most cases this actually what you need and certainly what you need in your case, as a user name of length zero does not make much sense.
这与 Python 非常接近:
这也可以使用正则表达式来解决,但上面的内容可能更清晰且不那么复杂。
That's very close to being Python:
This can also be solved with regular expressions, but the above is perhaps clearer and less complex.
您可以使用正则表达式轻松检查输入字符串:
使用
*
如果空字符串也是有效输入,则使用+
代替。正如其他答案中所建议的那样,使用 isalnum 也是一种不错的方法,但是使用正则表达式,您可以轻松调整检查,以防输入要求变得更加复杂。
You can easily check input strings using regular expressions:
Use
*
instead of+
if the empty string is valid input too.Using
isalnum
, as suggested in other answers, is a nice approach too, but with regular expressions you can easily adjust your check in case the requirements for input get more complex.实现这一点的一种方法是使用Python的正则表达式模块。它是一个标准库。
字符串前面的 r 不是拼写错误,它是将字符串视为原始字符串,您可能想要这样,而不是键入转义字符。
您可以参考此Python 2.7文档(或您选择的Python版本)
One way to achieve this is to use the regular expression module of Python. It is a standard library.
The r in front of the string is not a typo, it is to treat the string as raw, which you may want rather than typing escape characters.
You can refer to this Python 2.7 Documents (or your chosen version of Python)
您将来可能需要验证字符串的所有字符是否都存在于特定列表中。
可能是这样的:
如果没有正则表达式,结果
You may need in the future to verify that all the characters of a string are present in a particular list.
Without regex, it is possible like that:
result