简体   繁体   English

python中的正则表达式和输入验证

[英]Regex and Input validation in python

I'm trying to validate an input with regex to make sure the input is something like: 0,7 我正在尝试使用正则表达式验证输入,以确保输入类似于: 0,7

0 being the lowest number 0是最低的数字

7 being the highest number 7是最高的数字

they need to be separated with and "," 他们需要和“,”分开

I've tried using 我试过用了

re.match("[0-7,0-7]", input):

No luck 没运气

You need to do: 你需要这样做:

re.match("[0-7],[0-7]$", input):

In Regex, [...] is a character set. 在Regex中, [...]是一个字符集。 This means that, in your original pattern, you were looking for a single character that is either a comma or a digit in the range of 0 to 7. Adding 0-7 twice does nothing. 这意味着,在原始模式中,您正在查找单个字符,该字符可以是逗号或0到7范围内的数字。添加0-7两次不会执行任何操作。

Also, I don't know what input is, but if it is a variable, then you should change its name. 另外,我不知道input是什么,但如果它是变量,那么你应该更改它的名字。 Having a variable named input overshadows the built-in. 使用名为input的变量会遮盖内置函数。

If input is the built-in though, then you need to invoke it by adding () at the end: 如果input 内置的,那么你需要通过在末尾添加()来调用它:

re.match("[0-7],[0-7]$", input()):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM