re

re.match函数

re.match 尝试从字符串的起始位置匹配一个模式

re.match(pattern, string, flags=0)    //匹配的正则表达式,字符串,标志符(如:是否区分大小写,多行匹配等)

# 判断给定的字符串是否只包含字母、数字、或者下划线中的一个或多个,并且以字母或数字开头

import re
s = "qwert123"
if re.match('^[0-9a-zA-Z]', s)  and re.match('^[0-9a-zA-Z_]+$', s[1:]):
    print(True)
else:
    print(False)

^    匹配字符串的开始

+    重复一次或更多次

$    匹配字符串的结尾

参考文档:

https://www.runoob.com/python/python-reg-expressions.html

https://www.cnblogs.com/shenjianping/p/11647473.html

原文地址:https://www.cnblogs.com/yu121/p/14850936.html