简体   繁体   中英

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 being the lowest number

7 being the highest number

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. 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.

Also, I don't know what input is, but if it is a variable, then you should change its name. Having a variable named input overshadows the built-in.

If input is the built-in though, then you need to invoke it by adding () at the end:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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