所以我做了这个练习,但是解不出来:
我只能接受一个字符串,如果它是由数字和字母组成的,必须至少包含其中的一个;并且长度必须是6-8个字符。字符串只有一个单词。
第一部分很好,尽管我不确定是否使用match:
re.match('([a-zA-Z]+[0-9]+)', string)但是我不知道如何指定长度,它应该是数字和字母加起来的长度。这是行不通的,我猜也不应该这样做:
re.match('([a-zA-Z]+[0-9]+){6,8}', string)谢谢你的帮助。
发布于 2011-08-19 07:21:32
试试这个:
^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d]{6,8}$解释:
^ //The Start of the string
(?=.*\d) //(?= ) is a look around. Meaning it
//checks that the case is matched, but
//doesn't capture anything
//In this case, it's looking for any
//chars followed by a digit.
(?=.*[a-zA-Z]) //any chars followed by a char.
[a-zA-Z\d]{6,8}//6-8 digits or chars.
$ //The end of the string.https://stackoverflow.com/questions/7115298
复制相似问题