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
'''