2.2.正则表达式
2.2.1.正则表达式语法
2.2.2.Python正则表达式
2.2.2.1.字符集合
2.2.2.2.‘或’方法
2.2.2.3.匹配数字’\d’等价于[0-9]
2.2.2.4.‘\D’匹配非数字
2.2.2.5.’\w’匹配字母和数字
2.2.2.6.‘\W’匹配非字母和数字
2.2.2.7.‘\s’匹配间隔符
2.2.2.8.重复
2.2.2.9.精确匹配和最小匹配
2.2.3.match与search
2.2.3.1.字符串的替换和修改
2.2.3.2.split 切片函数。
2.2.3.3.’(?P…)'命名组

2.2.正则表达式

2.2.1.正则表达式语法


2.2.2.Python正则表达式

指定好匹配的模式-pattern
选择相应的方法-match,search等
得到匹配结果-group

re.match #从开始位置开始匹配,如果开头没有则无
re.search # 搜索整个字符串
re.findall # 搜索整个字符串,返回一个list

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12abc789'
pattern = re.compile(r'.')
print(re.findall(pattern, input))
"""
输出:
['自', '然', '语', '言', '处', '理', '很', '重', '要', ' ', '。', ' ', '1', '2', 'a', 'b', 'c', '7', '8', '9']
"""

2.2.2.1.字符集合

[abc]指定包含字符。
zA-Z]来指定所有英文字母的大小写。
[^a-zA-Z]指定不匹配所有英文字母。

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'[abc]')
print(re.findall(pattern, input))
'''
输出结果:
['b', 'c']
'''pattern = re.compile(r'[a-zA-Z]')
print(re.findall(pattern, input))
'''
输出结果:
['A', 'b', 'c']
'''pattern = re.compile(r'[^a-zA-Z]')
print(re.findall(pattern, input))
'''
输出结果:
['自', '然', '语', '言', '处', '理', '很', '重', '要', ' ', '。', ' ', '1', '2', '7', '8', '9']
'''

2.2.2.2.‘或’方法

将两个规则并列起来,以’|’连接,表示只满足其中之一就可以匹配。
[a-zA-Z]|[0-9]表示满足数字或字母就可以匹配,这个规则等价于[a-zA-Z0-9]

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'[a-zA-Z]|[0-9]')
print(re.findall(pattern, input))
'''
输出结果:
['1', '2', 'A', 'b', 'c', '7', '8', '9']
'''

2.2.2.3.匹配数字’\d’等价于[0-9]

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\d')
print(re.findall(pattern, input))

2.2.2.4.‘\D’匹配非数字

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\D')
print(re.findall(pattern, input))
'''
输出结果:
['自', '然', '语', '言', '处', '理', '很', '重', '要', ' ', '。', ' ', 'A', 'b', 'c']
'''

2.2.2.5.’\w’匹配字母和数字

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\w')
print(re.findall(pattern, input))
"""
输出结果:
['自', '然', '语', '言', '处', '理', '很', '重', '要', '1', '2', 'A', 'b', 'c', '7', '8', '9']
"""

2.2.2.6.‘\W’匹配非字母和数字

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\W')
print(re.findall(pattern, input))
"""
输出结果:
[' ', '。', ' ']
"""

2.2.2.7.‘\s’匹配间隔符

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\s')
print(re.findall(pattern, input))
"""
输出结果:
[' ', ' ']
"""

2.2.2.8.重复

正则式可以匹配不定长的字符串
‘*’ 0或次匹配

# -*- coding: UTF-8 -*-
import re
input = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\d*')
print(re.findall(pattern, input))
"""
输出结果:
['', '', '', '', '', '', '', '', '', '', '', '', '12', '', '', '', '789', '']
"""

‘+’ 1 次或多次匹配

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\d+')
print(re.findall(pattern, input))
"""
输出结果:
['12', '789']
"""

‘?’0或1次匹配

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\d?')
print(re.findall(pattern, input))
"""
输出结果:
['', '', '', '', '', '', '', '', '', '', '', '', '1', '2', '', '', '', '7', '8', '9', '']
"""

2.2.2.9.精确匹配和最小匹配

‘{m}’精确匹配m次

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\d{3}')
print(re.findall(pattern, input))
'''
输出结果:
['789']
'''

{m, n}匹配最少m次,最多n次。(n > m)

# -*- coding: UTF-8 -*-import reinput = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\d{1,3}')
print(re.findall(pattern,input))
'''
输出结果:
['12', '789']
'''

2.2.3.match与search

它们的返回不是一个简单的字符串列表,而是一个 MatchObject,可以得到更多的信息。
如果匹配不成功,它们则返回一个 NoneType 。所以在对匹配完的结果进行操作之前,必需先判断一下是否匹配成功了。
match从字符串的开头开始匹配,如果开头位置没有匹配成功,就算失败了;而 search 会跳过开头,继续向后寻找是否有匹配的字符串。

# -*- coding: UTF-8 -*-import reinput2 = '123自然语言处理'
pattern = re.compile(r'\d')
match = re.search(pattern, input2)
print(match.group())
'''
输出结果:
1
'''

2.2.3.1.字符串的替换和修改

在目标字符串中规则查找匹配的字符串,再把它们替换成指定的字符串。你可以指定一个最多替换次数,否则将替换所有的匹配到的字符串。
sub(rule, replace, target[, count])
subn(rule, replace, target[, count])
第一个参数是正则规则,第二个参数是指定的用来替换的字符串,第三个参数是目标字符串,第四个参数是最多替换次数。
sub 返回一个被替换的字符串
subn 返回一个元组,第一个元素是被替换的字符串,第二个元素是一个数字,表明产生了多少次替换。

# -*- coding: UTF-8 -*-import reinput2 = '123自然语言处理'
pattern = re.compile(r'\d')
print(re.sub(pattern,"数字",input2))
'''
输出结果:
数字数字数字自然语言处理
'''# -*- coding: UTF-8 -*-import reinput2 = '123自然语言处理'
pattern = re.compile(r'\d')
print(re.subn(pattern, '', input2))
'''
输出结果:
('自然语言处理', 3)
'''

2.2.3.2.split 切片函数。

使用指定的正则规则在目标字符串中查找匹配的字符串,用它们作为分界,把字符串切片
split(rule, target[, maxsplit])
第一个参数是正则规则,第二个参数是目标字符串,第三个参数是最多切片次数,返回一个被切完的子字符串的列表

# -*- coding: UTF-8 -*-import reinput3 = '自然语言处理123机器学习456深度学习'
pattern = re.compile(r'\d+')
print(re.split(pattern, input3))
'''
输出结果:
['自然语言处理', '机器学习', '深度学习']
'''

2.2.3.3.’(?P…)'命名组

<…> 里面给你这个组起的名字。

# -*- coding: UTF-8 -*-import reinput3 = '自然语言处理123机器学习456深度学习'
pattern = re.compile(r'(?P<dota>\d+)(?P<lol>\D+)')
m = re.search(pattern, input3)
print(m.group('lol'))
'''
输出结果:
机器学习
'''input = 'number 338-343-220'
pattern = re.compile(r'\d\d\d-\d\d\d-\d\d\d')
m = re.search(pattern, input)
print(m.group())
'''
输出结果:
338-343-220
'''

2.2.python正则表达式相关推荐

  1. Python 正则表达式各种特殊符号 重点

    Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. r ...

  2. python 正则表达式 re.compile() 的使用

    1 re.compile() 的解释: python 正则表达式 re.compile() 将正则表达式编译成一个Pattern规则对象,单独使用compile 没有意义,他生成的是一个规则,需要ma ...

  3. Python正则表达式,看这一篇就够了

    作者 | 猪哥 来源 | 裸睡的猪(ID: IT--Pig) 大多数编程语言的正则表达式设计都师从Perl,所以语法基本相似,不同的是每种语言都有自己的函数去支持正则,今天我们就来学习 Python中 ...

  4. Python 正则表达式

    最近研究Python爬虫,很多地方用到了正则表达式,但是没好好研究,每次都得现查文档.今天就专门看看Python正则表达式.本文参考了官方文档 re模块. 模式 首先正则表达式的语法我就不说了,这玩意 ...

  5. Python正则表达式初识(二)

    前几天给大家分享了[Python正则表达式初识(一)],介绍了正则表达式中的三个特殊字符"^"."."和"*",感兴趣的伙伴可以戳进去看看, ...

  6. python正则表达式需要模块_使用Python正则表达式模块,让操作更加简单

    处理文本数据的一个主要任务就是创建许多以文本为基础的特性. 人们可能想要在文本中找出特定格式的内容,比如找出存在于文本中的电子邮件,或者大型文本中的电话号码. 虽然想要实现上述功能听起来很繁琐,但是如 ...

  7. python正则表达式re.sub用法

    python正则表达式re.sub用法 https://cloud.tencent.com/developer/article/1382055 python正则表达式re.sub用法 全面的 http ...

  8. 【Python】一文读懂Python正则表达式常用用法

    点击上方"AI遇见机器学习",选择"星标"公众号 重磅干货,第一时间送达 编辑:爱学AI 来源:geekvi   链接: www.segmentfault.co ...

  9. Python正则表达式常用的15个符号整理

    http://blog.itpub.net/31403259/viewspace-2157778/ Python正则表达式常用的15个符号整理: 1. ? 匹配0次或一次前面的分组(问号在正则表达式中 ...

  10. Python正则表达式使用的四个基本步骤

    http://blog.itpub.net/31403259/viewspace-2157701/ Python正则表达式使用的四个基本步骤     1.用import re导入正则表达式模块     ...

最新文章

  1. Relay IR表示
  2. 调用存储过程时报错:There is no ‘username’@'host’ registered
  3. 通过mtd读写flash_Ambiq继续引领低功耗单片机,新款Apollo4将2MB MRAM作为Flash
  4. 机器学习中 True Positives(真正例TP)、False Positives(假正例FP)、True Negatives(真负例TN)和 False Negatives(假负例FN)指什么
  5. python find函数实现原理_非常干货:Python 探针实现原理
  6. Linux操作系统基础解析之(四)——Linux基本命令剖析(2)
  7. 嵌入式系统功能需求分析_机械管理系统如何物料需求分析
  8. 如何基于云通讯构建企业移动信息化竞争力
  9. png 微软ppt 透明度_工作必备,如何用PPT把logo玩出千种花样?
  10. 思科防火墙ASA5520做NAT映射配置实例
  11. 软件开发学习资料大全
  12. QFont所有支持字体
  13. 计算机视觉领域的一些牛人博客,超有实力的研究机构web主页
  14. Vue 2.0 开发聊天程序(二)真正的开始
  15. jQuery.ajax 使用点滴
  16. WIN10下系统缩放比例(DPI)的魔幻设置
  17. easypoi导入excel实践方法:
  18. python+selenium+chrome实现淘宝购物车秒杀自动结算
  19. Java代码清除Word文档的批注和修订 (Aspose.Words) Java老铁们,亲测有效!
  20. Python 爬取百度音乐

热门文章

  1. java调用其他方法中的变量_Uipath中调用Python的方法
  2. linux 命令行 java_在Linux上讲Java命令行的作为服务运行
  3. cleanmymac 4.2_15北师大版八年级物理上册4.2节乐音微课视频|知识点|练习
  4. VTK:多块数据集用法实战
  5. boost::remove相关的测试程序
  6. boost::leaf::exception用法的测试程序
  7. boost::intrusive::auto_unlink_hook用法的测试程序
  8. boost::geometry::strategy::vincenty用法的测试程序
  9. boost::fusion::result_of::size用法的测试程序
  10. GDCM:gdcm::StringFilter的测试程序