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)
['这是一段包含', '和中文的文本']
相关推荐
程序员的世界你不懂3 小时前
Appium+python自动化(八)- 认识Appium- 下章
python·appium·自动化
恸流失4 小时前
DJango项目
后端·python·django
Julyyyyyyyyyyy5 小时前
【软件测试】web自动化:Pycharm+Selenium+Firefox(一)
python·selenium·pycharm·自动化
蓝婷儿6 小时前
6个月Python学习计划 Day 15 - 函数式编程、高阶函数、生成器/迭代器
开发语言·python·学习
love530love6 小时前
【笔记】在 MSYS2(MINGW64)中正确安装 Rust
运维·开发语言·人工智能·windows·笔记·python·rust
水银嘻嘻6 小时前
05 APP 自动化- Appium 单点触控& 多点触控
python·appium·自动化
狐凄6 小时前
Python实例题:Python计算二元二次方程组
开发语言·python
烛阴7 小时前
Python枚举类Enum超详细入门与进阶全攻略
前端·python
Mikhail_G9 小时前
Python应用函数调用(二)
大数据·运维·开发语言·python·数据分析
weixin_472339469 小时前
使用Python提取PDF元数据的完整指南
java·python·pdf