import retext001 = "hello"
a = re.match('h',text001)
print(a.group())text002 = "hello"
a = re.match('.',text002) #匹配任意字符串
print(a.group())text003 = "11111"
a = re.match(r'\d', text003) #匹配任意数字
print(a.group())text004 = "+hello"
a = re.match(r'\D',text004) #匹配任意非数字
print(a.group())text005 = " hello"
a = re.match(r'\s',text005) #匹配空白字符串(\n,\t,\z,空格)
print(a.group())text006 = "_hello"
a = re.match(r'\w',text006) #匹配a-z,A-Z,数字和下划线
print(a.group())text007 = "+hello"
a=re.match(r'\W',text007) #与\w相反
print(a.group())print("*"*50)
#[]组合方式,只要满足[]的字符,就可以匹配
text008 = "0731-888888"
a= re.match(r'[\d\-]+',text008)
print(a.group())text009 = "0731-888888"
a= re.match(r'[0-9]+',text009)
print(a.group())text010 = "ask23dl46EFWF78a547W"
a= re.match(r'[a-z,A-Z,0-9]+',text010)
print(a.group())
输出:
h
h
1
+
(空字符串)
_
+
**************************************************
0731-888888
0731
ask23dl46EFWF78a547W