在 python 中对字符串应用过滤器

发布于 2024-10-19 07:53:33 字数 287 浏览 3 评论 0原文

我有一个用户输入他的用户名,我只想传递有效的字符串,这意味着仅 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小巷里的女流氓 2024-10-26 07:53:33

字符串中有一个名为isalnum的方法。它会实现您想要实现的目标。

In [7]: 'ab123fd'.isalnum()
Out[7]: True

In [8]: 'ab123fd **'.isalnum()
Out[8]: False

There is a method in string called isalnum. It does what you are trying to achieve.

In [7]: 'ab123fd'.isalnum()
Out[7]: True

In [8]: 'ab123fd **'.isalnum()
Out[8]: False
哑剧 2024-10-26 07:53:33

您需要 isalnum

>>> name = raw_input('Enter your name: ')
Enter your name: foo_bar
>>> name.isalnum()
False
>>> name = raw_input('Enter your name: ')
Enter your name: foobar
>>> name.isalnum()
True

You need isalnum:

>>> name = raw_input('Enter your name: ')
Enter your name: foo_bar
>>> name.isalnum()
False
>>> name = raw_input('Enter your name: ')
Enter your name: foobar
>>> name.isalnum()
True
撕心裂肺的伤痛 2024-10-26 07:53:33

Python 字符串有很多有用的方法来执行此类检查,例如:

  • str.isalnum()
  • str.isalpha()
  • str.isdigit()
  • str.islower()
  • str.istitle()
  • str.isupper()

你需要的是str.isalnum() 如果字符串中的所有字符都是字母数字并且至少有一个字符,则返回 true。

>>> 'hello1'.isalnum()
True
>>> 'hello 1'.isalnum()
False
>>> 'hello!'.isalnum()
False
>>> ''.isalnum()
False

如上面的示例所示,字母和数字被视为字母数字,但空格和标点符号则不然。

另请注意,与纯数学相反,空字符串不被视为字母数字。然而,在大多数情况下,这实际上是您所需要的,并且肯定是您所需要的,因为长度为零的用户名没有多大意义。

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.

>>> 'hello1'.isalnum()
True
>>> 'hello 1'.isalnum()
False
>>> 'hello!'.isalnum()
False
>>> ''.isalnum()
False

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.

給妳壹絲溫柔 2024-10-26 07:53:33

这与 Python 非常接近:

def _checkInput(input):
     for c in input:
          if not (c in string.ascii_letters or c in string.digits):
               return False
     return True

这也可以使用正则表达式来解决,但上面的内容可能更清晰且不那么复杂。

That's very close to being Python:

def _checkInput(input):
     for c in input:
          if not (c in string.ascii_letters or c in string.digits):
               return False
     return True

This can also be solved with regular expressions, but the above is perhaps clearer and less complex.

哥,最终变帅啦 2024-10-26 07:53:33

您可以使用正则表达式轻松检查输入字符串:

>>> import re
>>> s = getinput()
>>> if not re.match(r'^[a-zA-Z0-9]+

使用*如果空字符串也是有效输入,则使用 + 代替。

正如其他答案中所建议的那样,使用 isalnum 也是一种不错的方法,但是使用正则表达式,您可以轻松调整检查,以防输入要求变得更加复杂。

, s) ... print "bad input"

使用*如果空字符串也是有效输入,则使用 + 代替。

正如其他答案中所建议的那样,使用 isalnum 也是一种不错的方法,但是使用正则表达式,您可以轻松调整检查,以防输入要求变得更加复杂。

You can easily check input strings using regular expressions:

>>> import re
>>> s = getinput()
>>> if not re.match(r'^[a-zA-Z0-9]+

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.

, s) ... print "bad input"

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.

半枫 2024-10-26 07:53:33

实现这一点的一种方法是使用Python的正则表达式模块。它是一个标准库。

import re

_pmatcher = re.compile(r'[0-9a-zA-Z]*

字符串前面的 r 不是拼写错误,它是将字符串视为原始字符串,您可能想要这样,而不是键入转义字符。

您可以参考此Python 2.7文档(或您选择的Python版本)

) def _checkInput(input): return _pmatcher.match(input)

字符串前面的 r 不是拼写错误,它是将字符串视为原始字符串,您可能想要这样,而不是键入转义字符。

您可以参考此Python 2.7文档(或您选择的Python版本)

One way to achieve this is to use the regular expression module of Python. It is a standard library.

import re

_pmatcher = re.compile(r'[0-9a-zA-Z]*

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)

) def _checkInput(input): return _pmatcher.match(input)

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)

狼亦尘 2024-10-26 07:53:33

您将来可能需要验证字符串的所有字符是否都存在于特定列表中。

可能是这样的:

ch = 'becdi30!&'

okchars = 'abcdefghijk012345,;:!&-'

print all(c in okchars for c in ch)

如果没有正则表达式,结果

True

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:

ch = 'becdi30!&'

okchars = 'abcdefghijk012345,;:!&-'

print all(c in okchars for c in ch)

result

True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文