2.2.python正则表达式

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 re

input = '自然语言处理很重要 。 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 re

input = '自然语言处理很重要 。 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 re

input = '自然语言处理很重要 。 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 re

input = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\d')
print(re.findall(pattern, input))
2.2.2.4.‘\D’匹配非数字
# -*- coding: UTF-8 -*-

import re

input = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\D')
print(re.findall(pattern, input))
'''
输出结果:
['自', '然', '语', '言', '处', '理', '很', '重', '要', ' ', '。', ' ', 'A', 'b', 'c']
'''
2.2.2.5.’\w’匹配字母和数字
# -*- coding: UTF-8 -*-

import re

input = '自然语言处理很重要 。 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 re

input = '自然语言处理很重要 。 12Abc789'
pattern = re.compile(r'\W')
print(re.findall(pattern, input))
"""
输出结果:
[' ', '。', ' ']
"""
2.2.2.7.‘\s’匹配间隔符
# -*- coding: UTF-8 -*-

import re

input = '自然语言处理很重要 。 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 re

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

‘?’0或1次匹配

# -*- coding: UTF-8 -*-

import re

input = '自然语言处理很重要 。 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 re

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

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

# -*- coding: UTF-8 -*-

import re

input = '自然语言处理很重要 。 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 re

input2 = '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 re

input2 = '123自然语言处理'
pattern = re.compile(r'\d')
print(re.sub(pattern,"数字",input2))
'''
输出结果:
数字数字数字自然语言处理
'''

# -*- coding: UTF-8 -*-

import re

input2 = '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 re

input3 = '自然语言处理123机器学习456深度学习'
pattern = re.compile(r'\d+')
print(re.split(pattern, input3))
'''
输出结果:
['自然语言处理', '机器学习', '深度学习']
'''
2.2.3.3.’(?P…)'命名组

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

# -*- coding: UTF-8 -*-

import re

input3 = '自然语言处理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
'''
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值