您的位置:首页 > 健康 > 养生 > python 正则表达式

python 正则表达式

2024/10/6 16:16:07 来源:https://blog.csdn.net/qq_41122834/article/details/140474750  浏览:    关键词:python 正则表达式

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

>>>input = '自然语言处理很重要, 123abc456'
>>>import re
>>>pattern = re.compile('.')
>>>re.findall(pattern,input)
['自', '然', '语', '言', '处', '理', '很', '重', '要', ',', ' ', '1', '2', '3', 'a', 'b', 'c', '4', '5', '6']
>>>pattern = re.compile(r'[abc]')
>>>re.findall(pattern,input)
['a', 'b', 'c']
>>>pattern = re.compile(r'[a-zA-Z]')
>>>re.findall(pattern,input)
['a', 'b', 'c']>>>pattern = re.compile(r'[^abc]')
>>>re.findall(pattern,input)
['自', '然', '语', '言', '处', '理', '很', '重', '要', ',', ' ', '1', '2', '3', '4', '5', '6']>>>pattern = re.compile(r'[abc]|[0-9]')
>>>re.findall(pattern,input)
['1', '2', '3', 'a', 'b', 'c', '4', '5', '6']>>>pattern = re.compile(r'\d')
>>>re.findall(pattern,input)
['1', '2', '3', '4', '5', '6']
>>>pattern = re.compile(r'\D')
>>>re.findall(pattern,input)
['自', '然', '语', '言', '处', '理', '很', '重', '要', ',', ' ', 'a', 'b', 'c']
>>>pattern = re.compile(r'\w')
>>>re.findall(pattern,input)
['自', '然', '语', '言', '处', '理', '很', '重', '要', '1', '2', '3', 'a', 'b', 'c', '4', '5', '6']
>>>pattern = re.compile(r'\W')
>>>re.findall(pattern,input)
[',', ' ']
>>>pattern = re.compile(r'\d{3}')
>>>re.findall(pattern,input)
['123', '456']
>>>pattern = re.compile(r'\d{2}')
>>>re.findall(pattern,input)
['12', '45']
>>>pattern = re.compile(r'\d{2,3}')
>>>re.findall(pattern,input)
['123', '456']

match与search
match从字符串开头匹配,如果开头位置没有匹配成功就算失败;而search会跳过开头,继续向后寻找是否有匹配的字符串。

>>>input2 = '123自然语言处理66'
>>>pattern = re.compile(r'\d')
>>>match =re.search(pattern,input2)
>>>match.group()
'1'
>>>pattern = re.compile(r'\d{3}')
>>>match =re.search(pattern,input2)
>>>match.group()
'123'

字符串的替换与修改
sub(rule,replace,target[,count])
subn(rule,replace,target[,count])
count匹配次数
sub返回一个被替换的字符串
subn返回一个元组

>>>input2 = '123自然语言处理66'
>>>pattern = re.compile(r'\d')
>>>re.sub(pattern,'数字',input2)
'数字数字数字自然语言处理数字数字'
>>>pattern = re.compile(r'\d{2,3}')
>>>re.sub(pattern,'数字',input2)
'数字自然语言处理数字'
>>>re.sub(pattern,'数字',input2,1)
'数字自然语言处理66'
>>>re.subn(pattern,'数字',input2,1)
('数字自然语言处理66', 1)
>>>re.subn(pattern,'数字',input2)
('数字自然语言处理数字', 2)

**split切片函数,**使用指定的正则规则在目标字符串中查找匹配的字符串,用他们作为分界,返回一个被切完的字符串列表

>>>input3 = '自然语言123自然语言23自然语言65'
>>>pattern = re.compile(r'\d+')
>>>re.split(pattern,input3)
['自然语言', '自然语言', '自然语言', '']

‘(?P<…>)’命名组

>>>input3 = '自然语言123自然语言23自然语言65'
>>>pattern = re.compile(r'(?P<data>\d+)(?P<cont>\D+)')
>>>re.search(pattern,input3)
>>>re.Match object; span=(4, 11), match='123自然语言'>
>>>gr = re.search(pattern,input3)
>>>gr.groups()
('123', '自然语言')
>>>gr.group('data')
'123'

中文匹配 [\u4e00-\u9fff]

>>>text = '这是一段包含english和中文的文本'
>>>pattern = re.compile(r'[\u4e00-\u9fff]+')
>>>pattern.findall(text)
['这是一段包含', '和中文的文本']

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com