简体   繁体   中英

TypeError: match() takes from 2 to 3 positional arguments but 5 were given

Full Error:

Traceback (most recent call last):
  File "N:/Computing (Programming)/Code/name.py", line 3, in <module>
    valid = re.match("[0-9]","[0-9]","[A-Z]","[a-z]" ,tutorGroup)
TypeError: match() takes from 2 to 3 positional arguments but 5 were given

My code:

import re
tutorGroup = input("Enter your tutor group - e.g. 10: ")
valid = re.match("[0-9]","[0-9]","[A-Z]","[a-z]" ,tutorGroup)
if valid:
        print("OK!")
else:
        print("Invalid!")

I'm trying to search a string with a given parameter

The problem is, that re.match take 2 or 3 arguments, not 5. First the regex pattern, and the string to match. optionally it takes a 3rd argument with flags. If you want to match a single digit or a letter, you would use [0-9a-zA-Z] as regex. If you want multiple letters or digits, you can use [0-9a-zA-Z]+ . If you want a list of digits or a list of letters (but not mixed), you can use ([0-9]+)|[a-zA-Z]+ .

Edit: After reading your comment, the regex you want is [0-9]{2}[a-zA-Z]{2}

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