Python专题1-使用正则表达式

举报
北京小马哥 发表于 2019/05/21 00:49:29 2019/05/21
【摘要】 主要介绍正则表达式在Python中的实现,re模块的主要函数.本文后续不断添加新内容,迭代更新.

本文是小马哥边学习边记录的笔记,后面的过程中也会不断修改迭代加入新的内容,希望与大家多多交流;作为程序员,直接以代码的方式说明实例,读者尽管打开你的IDE或者JupyterNotebook运行文中代码.


本文重点: 介绍正则表达式的主要函数方法

import re

#(1)使用match()方法匹配字符串

# match()在字符串的其实部分进行匹配: 如果匹配成功,返回一个匹配对象;如果匹配失败,返回一个None
# group()方法能够用于显示成功的匹配
result = re.match(pattern ='hello',string ='hello python')    # 返回一个re.Match对象
if result is not None:
    print(result.group())#返回成功的匹配
#match特点: 在字符串的起始位置开始匹配,即使字符串比模式长也会match成功,

#(2)使用search()方法在字符串中查找

# 不是所有的字符串都是在开头部分进行匹配,更多的情况是搜索字符串中是否包含指定的模式/模板

# search方法与match方法的不同在于,match是在字符串的开头进行匹配,而search可以搜索字符串的任意位置,只有遇到一次匹配就返回匹配对象,否则返回None

#search()方法会自左到右一次匹配字符串,知道得到一次出现的位置

m = re.match('python','hello python')
if m is not None:print('Match(): ',m.group())
s = re.search('python','hello python')
if s is not None:print('Search(): ',s.group())

#(3)匹配对个模式: 择一匹配符号的使用

patterns ='Mark|Jack|Pony'
result = re.match(patterns,'Pony Ma')
if result is not None:
    print(result.group())
result = re.search(patterns,'Alibaba,Jack Ma')
if result is not None:
    print(result.group())

#(4)匹配任何单个字符

#注意不能匹配换行符或者非字符(空字符串)

msg ='hello go'
pattern ='g.'
result = re.search(pattern,msg)
if result is not None:
    print(result)
    print(result.group())
msg2 ='hello \nyou'
pattern ='.you'
result = re.search(pattern,msg2)
if result is None:
    print(result)

#注意: 如果想匹配标点:".",要通过使用转移符号\来解决

msg3 ='nlgc@163.com'
pattern ='\.com'  #如果一定要匹配一个".",那么要加上\,否则,就会任意符合条件的字符串都会被匹配到
result = re.search(pattern,msg3)
if result is not None:
    print(result.group())

#(5)创建字符集

pattern ='[abcd]'
msg ='x'
result = re.match(pattern,msg)
print(type(result))

#(6)特殊字符( ., \d, \w, \s)与分组以及频数(+, *, ?)的使用

#匹配电子邮件xxxxx@yyyyy.com

pattern ='\w+@(.)*?.com'
email ='mark123@yeah.163.com'
result = re.match(pattern,email)
print(type(result))

#(7)匹配字符串的起始和结尾以及单词边界

#一般这种位置用于search()中,在字符串中搜索出符合模式的字符串,因为match()就是在字符串的起始位置开始的

msg ='Lost years are worse than lost dollars.'
pattern ='^Lost(.*)$'
result = re.match(pattern,msg)
if result is not None:
    print(result.groups())

#(8)使用findall()查找每一次出现的位置

#findall(): 查询字符串中某个正则表达式模式全部匹配情况,结果包含在列表里面返回

msg ='hello1 python,hello2 java,hello3 go'
pattern ='hello\d'
result = re.findall(pattern,msg)
print(result)

#(9)使用finditer()更加节省内存

#(10)使用sub()和subn()替换字符串中符合模式的子字符串

#subn()和sub()功能一样,但是多了一个返回被替换的个数

#将welcome变量里面的name换做name变量里面的字符串Mark

name ='Mark'
welcome ='Good morning,name!'
result = re.sub('name',name,welcome)
print(result)
result_plus = re.subn('name',name,welcome)
print(result_plus)#同sub()一样会返回替换完成之后的句子,但是作为元组,还会返回被修改的模式的次数

#(11)split()分割字符串

data ='邮政编码:100000'
pattern =':'
result = re.split(pattern,data)
print(result)


【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200