所以我有这个练习,无法解决它:
我只能接受一个字符串,如果它由数字和字母构成,则必须至少包含其中一个字符串;它必须是6-8个字符长.字符串只有一个字.
第一部分很好,虽然我不确定使用匹配:
re.match('([a-zA-Z]+[0-9]+)', string)
但我不知道如何指定长度应该是加起来的数字和字母的长度.这不起作用,我想不管怎么说:
re.match('([a-zA-Z]+[0-9]+){6,8}', string)
谢谢你的帮助.
最佳答案 试试这个:
^(?=.*d)(?=.*[a-zA-Z])[a-zA-Zd]{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-Zd]{6,8}//6-8 digits or chars.
$ //The end of the string.
最后
以上就是英勇草莓最近收集整理的关于python字符串里汉字和字母的长度相同_python – 匹配数字和字母以及特定长度的字符串...的全部内容,更多相关python字符串里汉字和字母的长度相同_python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复