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)
['这是一段包含', '和中文的文本']
相关推荐
AIAdvocate1 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼1 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
FreakStudio3 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal5 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali5 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ5 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.6 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python
Trouvaille ~6 小时前
【Python篇】深度探索NumPy(下篇):从科学计算到机器学习的高效实战技巧
图像处理·python·机器学习·numpy·信号处理·时间序列分析·科学计算
爆更小小刘6 小时前
Python基础语法(3)下
开发语言·python
哪 吒6 小时前
华为OD机试 - 第 K 个字母在原来字符串的索引(Python/JS/C/C++ 2024 E卷 100分)
javascript·python·华为od