Python正则表达式入门指南

import re

s = "hello world"
result = re.match("hello",s)
print(result)
print(result.span())
print(result.group())

import re

s = "hello world"
result = re.match("hello",s)
print(result)
print(result.span())
print(result.group())
s = "good hello world"
result = re.search("hello",s)
print(result)


s = "good hello world  hello earth"
result = re.findall("hello",s)
print(result)

import re

s = "hello world"
result = re.match("hello",s)
print(result)
print(result.span())
print(result.group())
s = "good hello world"
result = re.search("hello",s)
print(result)


s = "good hello world  hello earth"
result = re.findall("hello",s)
print(result)

s = "aaajkjl dja123jkjhj#hjkhd3444@"
result = re.findall(r'\d',s)#r 表示是正常子符
print(result)
result = re.findall(r'\W',s)# 特殊字符
print(result)

result = re.findall(r'[a-zA-Z 2-3]',s)
print(result)

#^ 开头 结尾
r = '^[0-9a-zA-Z]{6,10}$'
#满足条件
s = "123456yys"
result = re.findall(r,s)
print(result)
#不满足条件
s = "123456yys*"
result = re.findall(r,s)
print(result)


r = '^[1-9][0-9]{4,10}$'
#满足条件
s = "35878986"
result = re.findall(r,s)
print(result)
#不满足条件
s = "035878986"
result = re.findall(r,s)
print(result)

r = r'(^[\w-]+(\.[\w-]*)*@(qq|163|gmail)(\.[\w-]*)+$)'
s = "a.b.e.f.g@qq.com.e.c"
result = re.findall(r,s)
print(result)
result = re.match(r,s)
print(result)

s = "a.b.e.f.g@123.com.e.c"
result = re.findall(r,s)
print(result)
result = re.match(r,s)
print(result)

物联沃分享整理
物联沃-IOTWORD物联网 » Python正则表达式入门指南

发表评论