re正则表达式匹配单个字符串

03-01 1107阅读

import re
text001 = "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())

输出:

re正则表达式匹配单个字符串
(图片来源网络,侵删)

h

h

1

+

       (空字符串)

_

+

**************************************************

0731-888888

0731

ask23dl46EFWF78a547W

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]