Python-正则表达式

Python 1.5 以后增加了正则re模块,提供可正则表达式功能
re 模块使得python拥有全部的正则表达式的功能
import re
'''
正则表达式:用一些特殊字符组哼成一定规则的字符串
正则的作用:1、判断某个字符串是否符合某种规则
            2、从字符串中截取符合规则的子字符串
'''
'''
re.match(pattern,string,flags=0)
    pattern:正则表达式
    string:需要匹配的字符串
    flags:标志位,设置一些特殊功能,如是否区分大小写
    re.I:忽略大小写
    re.M:多行匹配,回影响^和$
    re.S:会匹配包括换行符在内的所有字符
从字符串的开头开始匹配,如果匹配成功,返回一个特殊的对象;如果匹配失败,返回None
'''
tmp = 'http'  # 正则表达式  判断字符串里面有没有'http'这个字符串
print(re.match(tmp, 'http://www.baidu.com'))
print(re.match(tmp, 'www.baidu.com'))
print(re.match(tmp, 'www.http.baidu.com'))
print(re.match(tmp, 'Http://www.baidu.com'))
print(re.match(tmp, 'Http://www.baidu.com', flags=re.I))

print(re.match(tmp, 'Http://www.baidu.com', flags=re.I).span())

'''
re.search(pattern,string,flags=0)
    pattern:正则表达式
    string:需要匹配的字符串
    flags:标志位,设置一些特殊功能,如是否区分大小写
功能:扫描整个字符串,并返回第一个匹配成功的结果。如果匹配不成功,返回None。

search与match的区别:match匹配开头,searc匹配整个字符串

'''
print('------------search---------------------')
print(re.search(tmp, 'http://www.baidu.com'))
print(re.search(tmp, 'www.baidu.com'))
print(re.search(tmp, 'www.http.baidu.com'))
print(re.search(tmp, 'www.http.baidu.http.com'))
print(re.search(tmp, 'www.Http.baidu.http.com'))
print(re.search(tmp, 'www.Http.baidu.http.com', flags=re.I))


print('--------find-----------')
'''
re.findall(pattern,string,flags=0)
    pattern:正则表达式
    string:需要匹配的字符串
    flags:标志位,设置一些特殊功能,如是否区分大小写
功能:匹配整个字符串,返回正则表达式在字符串中匹配的所有结果的列表;
      如果没有匹配结果,返回空列表
'''
print(re.findall(tmp, 'http://www.baidu.com'))
print(re.findall(tmp, 'http://www.http.baidu.com.http'))
print(re.findall(tmp, 'http://www.HTtp.baidu.com.hTTp', flags=re.I))
print(re.findall(tmp, 'tp://www.tp.baidu.com.hTp', flags=re.I))


'''
re.finditer(pattern,string,flags=0)
    pattern:正则表达式
    string:需要匹配的字符串
    flags:标志位,设置一些特殊功能,如是否区分大小写
返回一个迭代器,记录多个匹配的结果,扫描整个字符串
'''

result = re.finditer(tmp, 'http://www.http.Http', flags=re.I)
print(result)
for i in result:
    print(i)

正则元字符(1)

import re
'''
.              匹配除换行符以外的所有字符
[0123456789]   []是字符集合,匹配括号中包含的任意一个字符
[god]          匹配god中的任意一个字符
[a-z]          匹配任意一个小写字母
[A-Z]          匹配任意一个大写字母
[0-9]          匹配任意一个数字   [0123456789]
[0-9a-zA-Z]    匹配任意数字及字母
[0-9a-zA-Z_]   匹配任意数字及字母及下划线
[^0-9]         除数字以外的字符
[^god]         匹配除god中的字符
\d             匹配数字  效果等同于[0-9]
\D             匹配非数字  效果等同于[^0-9]
\w             匹配数字、字母及下划线  效果等同于[0-9a-zA-Z_]
\W             匹配非数字、字母及下划线  效果等同于[^0-9a-zA-Z_]
\s             匹配任意空白符    效果等同于[ \n\t\r\f]  (空格,换行,制表,回车,换页)
\S             匹配任意的非空白符    效果等同于[^ \n\t\r\f]
出现在方括号最前面的^称为脱字符,表示不匹配集合中的字符
出现在方括号里其他位置的的^只是普通字符
'''

tmp = '[a-z]'
print(re.findall(tmp, 'qwearcty'))
print(re.findall('[0-9]','46dfghjkl0'))
print(re.findall('[0-9a-zA-Z]', '12sd#$%'))
print(re.findall('[^0-4]', '0123456789'))
print(re.findall('[abc^def]', 'abcqwe^'))
print(re.findall('[^0-9a-f^qw]', 'abc98$^qwt'))  # $ t
print(re.findall('\D', 'qwe123tyu'))
print(re.findall('\W', 'qwe123tyu_@#$%'))
print(re.findall('\S', 'abc qwe\n'))

正则元字符(2)

import re
'''
^    行首匹配(不在方括号内的)
$    行尾匹配
设置flags=re.M才能进行多行匹配,否则只匹配整个字符串的开头或结尾

\A   匹配行首,与^的区别:\A只匹配整个字符串的开头,即使在flags=re.M模式下,
     也不会匹配其他行的行首
\Z   匹配行尾,与$的区别:\A只匹配整个字符串的结尾,即使在flags=re.M模式下,
     也不会匹配其他行的行尾
     
\b   匹配字符边界,就是字符串与空格间的位置   'er\b' 可以匹配nicer,不能匹配nierce
\B   匹配非字符边界

'''
tmp = '^nice'
print(re.findall('^nice', 'nice to meet you too'))
print(re.findall('^nice', 'nice to meet you\nnice to meet'))
print(re.findall('^nice', 'to meet nice you too'))
print(re.findall('^nice', 'nice to meet you\nnice to meet', flags=re.M))

print('-----------------------------')
tmp = 'you$'
print(re.findall('you$', 'nice you'))
print(re.findall('you$', 'nice you\nnice you you'))
print(re.findall('you$', 'nice you\nnice you yu'))
print(re.findall('you$', 'nice you\nnice you you', flags=re.M))

print('---------------------------------')
# '\Anice'    'you\Z'
print(re.findall('\Anice', 'nice you\nnice you', flags=re.M))
print(re.findall('you\Z', 'nice you\nnice you', flags=re.M))

print('*******************************')
print(re.findall(r'ce\b', 'nicer you ces ce you'))
print(re.findall(r'\bce\b', 'nice you ces ce you'))

print('*******************************')
print(re.findall(r'ce\B', 'nice you ces ce you'))
print(re.findall(r'\Bce\B', 'nice you dces ce you'))



# print(re.findall(r'ce\b', 'nice you nicer ce you ces'))



# QQ  邮箱   手机号   电话号   网址



正则元字符(3)

import re
'''
(abc)    匹配小括号中的字符abc (将abc作为一个整体匹配)
x?       ? 匹配0个或1个x   (x是任意字符串)   非贪婪匹配
x+       匹配至少一个x   贪婪匹配
x*       匹配任意多个    贪婪匹配
x{n}     匹配确定的n个x   (n是一个非负整数)
x{n,}    匹配至少n个x   (n是一个非负整数)
x{n,m}   匹配至少n个x,最多m个x   n<=m   (n,m是一个非负整数)

x|y      匹配x或y    | 表示或
'''

print(re.findall('[abc]','aqbecrabcrty'))
print(re.findall('(abcd)','aqbecrabcrty'))

print('?????????????????')
print(re.findall('(aa)?', 'bbb'))
print(re.findall('(aa)?', 'aab'))
print(re.findall('(aa)?', 'aaab'))
print(re.findall('(aa)?', 'aaaab'))
print('++++++++++++++++')
print(re.findall('aa+', 'bbb'))
print(re.findall('aa+', 'abb'))
print(re.findall('aa+', 'aabb'))
print(re.findall('aa+', 'aaaabb'))
print(re.findall('ba+', 'aabaaabaaaa'))


print('***************')
print(re.findall('a*', 'aabbabbaaaaa'))


print('~~~~~~~~~~~~~~~~~~~~~~~~~~~')
# 123456     6-8
print(re.findall('a{2}', 'aaabbaa'))
print(re.findall('a{2,}', 'aabbabbaaaaaa'))
print(re.findall('a{3,5}', 'aabbabbaaaa'))

# 'good----Good'
print(re.findall('((g|G)ood)', 'good---Good'))
# ((g|G)ood)  ===>   good    Good




正则元字符(4)

import re
'''
特殊元字符
.*?     将贪婪匹配变为非贪婪匹配
.       匹配除换行符以外的所有字符
*       匹配任意多个    贪婪匹配
?       匹配任意0个或1个   非贪婪匹配
'''
tmp = 'who is .* ha'
str1 = 'who is girl ha who is boy ha who is man ha'
print(re.findall(tmp, str1))   # []

tmp = 'who is .*? ha'
str1 = 'who is girl ha who is boy ha who is man ha'
print(re.findall(tmp, str1))   # []



正则元字符(5)

import re
'''
分组:group
除了简单的判断是否匹配之外,正则表达式还有提取子串的功能。
用()表示的就是提取的分组。从外向里,从左向右标注第几组
'''
# 010-12345678
# \d{3}-\d{8}
tmp = '\d{3}-\d{8}'
s = '010-12345678'
result = re.findall(tmp, s)
print(result)

temp2 = '(\d{3})-(\d{8})'    # 用小括号括起来的为一组
result2 = re.findall(temp2, s)
print(result2)

tmp3 = '(\d{3})-(\d{8})'
result3 = re.match(tmp3, s)
# print(result3)
# groups : 查看匹配的分组的情况
print(result3.groups())
# group 可以单独获取分组情况  group(0) 一直代表原始字符串
print(result3.group())
print(result3.group(0))
print(result3.group(1))
print(result3.group(2))



tmp4 = '(?P<first>\d{3})-(?P<second>\d{8})'
result4 = re.match(tmp4, s)
print(result4.group(0))
print(result4.group(1))
print(result4.group('first'))
print(result4.group('second'))

# a3_.a5_@qw.com

tmp5 = '^(\w+)\.(\w+)@(([a-zA-Z]{2})(\.com))$'
# 从外向里,从左向右标注第几组
# ^(\w+)\.(\w+)@(([a-zA-Z]{2})(\.com))$
# (\w+)
# (\w+)
# (([a-zA-Z]{2})(\.com))
# ([a-zA-Z]{2})
# (\.com)

s2 = 'a3.a5@er.com'
result5 = re.match(tmp5, s2)
print(result5.groups())
print(result5.group(0))
print(result5.group(5))


# 分割字符串
str3 = 'good    love  nice girl'   # [good love nice girl]
print(str3.split(' '))  # ['good', '', '', '', 'love', '', 'nice', 'girl']

result5 = re.split(' +', str3)
print(result5)


# 替换元素
# re.sub   re.subn
'''
re.sub(pattern, rep1, string)
pattern:正则表达式
rep1 : 替换的字符
string:操作的字符串
'''
res1 = re.sub(' +', '*', str3)
print(res1)
res2 = re.sub(' +', '*', str3, count=2)
print(res2)

# 将新的字符串和替换次数放到元组中返回
res3 = re.subn(' +', '*', str3)
print(res3)


# compile
tmp3 = '(\d{3})-(\d{8})'
result3 = re.match(tmp3, s)
print(result3)
'''
re.compile(pattern,flags=0)
作用:编译正则表达式,用于直接匹配对象
'''
tmp3 = '(\d{3})-(\d{8})'
re_temp3 = re.compile(tmp3)
res4 = re_temp3.match(s)
print(res4)


# 常用正则
# https://www.cnblogs.com/Akeke/p/6649589.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值