匹配多字符
{}*+都是用来表示限定位数
{}在里面限定
*表示至少有0个
+表示至少有1个
.*表示匹配至少0个除了\n换行符以外的符号
.+表示匹配至少1个除\n换行符以外的符号
[a-zA-Z]+表示至少有1个大小写字母
import re
result = re.match(r"[a-zA-Z]+","python3")
print(result)
运行结果:
<re.Match object; span=(0, 6), match=‘python’>
[a-zA-Z0-9]*表示至少有0个大小写字母和数字
import re
result = re.match(r"[a-zA-Z0-9]*","python3#")
print(result)
运行结果:
<re.Match object; span=(0, 7), match=‘python3’>
^表示以谁开头
$表示以谁结尾
^\d+表示以数字开头,至少一个
^ [a-zA-Z]表示以字母当做开头
import re
result = re.match(r"^[a-zA-Z0-9]","python3#")
print(result)
运行结果:
<re.Match object; span=(0, 1), match=‘p’>
[a-zA-Z0-9_]*$表示从前到后都必须是字母和数字、下划线
import re
result = re.match(r"[a-zA-Z0-9_]*$","python_3")
print(result)
运行结果:
<re.Match object; span=(0, 8), match=‘python_3’>