Python 正则表达式限定文本长度
Regex Expression
# Limit the string length between 1 and 10 letters (a-z, A-Z) ^[a-zA-Z]{1,10}$ eg. match: abceddddd no match: aaaaaaaaaaa # Limit the lenght of an arbitrayry pattern ^(?=[Ss]{1,10}$)[Ss]* eg. match: a-$%^&**u1 no match: a-$%^&**u1aa # Limit the length of nonwhitespace characters (whitespace may exist among nonwhitespace characters) ^s*(?:Ss*){1,10}$ eg. match: 2008 ddddd d no match: 2008 ddddd dd # Limit the number of words ^W*(?:w+W*){1,10}$ eg. match: abd dd22dd ddd ddd dd dd dd d1d _d dd no match: abd dd22dd ddd ddd dd dd dd d1d _d dd dd
CODE:
import re if re.match(r"^W*(?:w+W*){1,10}$", subject): # Successful match at the start of the string else: # Match attempt failed
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。