python 正则表达式



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会跳过开头,继续向后寻找是否有匹配的字符串。

python 复制代码
>>>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返回一个元组

python 复制代码
>>>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切片函数,**使用指定的正则规则在目标字符串中查找匹配的字符串,用他们作为分界,返回一个被切完的字符串列表

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

'(?P<...>)'命名组

python 复制代码
>>>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]

python 复制代码
>>>text = '这是一段包含english和中文的文本'
>>>pattern = re.compile(r'[\u4e00-\u9fff]+')
>>>pattern.findall(text)
['这是一段包含', '和中文的文本']
相关推荐
XIE39224 分钟前
Browser-use使用教程
python
酷爱码1 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
蹦蹦跳跳真可爱5891 小时前
Python----深度学习(基于深度学习Pytroch簇分类,圆环分类,月牙分类)
人工智能·pytorch·python·深度学习·分类
MinggeQingchun4 小时前
Python - 爬虫-网页解析数据-库lxml(支持XPath)
爬虫·python·xpath·lxml
Python自动化办公社区6 小时前
Python 3.14:探索新版本的魅力与革新
开发语言·python
weixin_贾6 小时前
最新AI-Python机器学习与深度学习技术在植被参数反演中的核心技术应用
python·机器学习·植被参数·遥感反演
张槊哲7 小时前
函数的定义与使用(python)
开发语言·python
船长@Quant7 小时前
文档构建:Sphinx全面使用指南 — 实战篇
python·markdown·sphinx·文档构建
偶尔微微一笑8 小时前
AI网络渗透kali应用(gptshell)
linux·人工智能·python·自然语言处理·编辑器
船长@Quant9 小时前
文档构建:Sphinx全面使用指南 — 基础篇
python·markdown·sphinx·文档构建