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)
['这是一段包含', '和中文的文本']

相关推荐

  1. Python表达式

    2024-07-17 06:12:02       48 阅读
  2. Python 表达式

    2024-07-17 06:12:02       37 阅读
  3. python表达式

    2024-07-17 06:12:02       39 阅读
  4. Python表达式

    2024-07-17 06:12:02       27 阅读
  5. Python-表达式

    2024-07-17 06:12:02       28 阅读
  6. python 表达式

    2024-07-17 06:12:02       25 阅读
  7. python 表达式

    2024-07-17 06:12:02       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 06:12:02       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 06:12:02       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 06:12:02       57 阅读
  4. Python语言-面向对象

    2024-07-17 06:12:02       68 阅读

热门阅读

  1. MySQL零散拾遗(二)

    2024-07-17 06:12:02       24 阅读
  2. chrome扩展清除指定站点缓存chrome.browsingData.remove

    2024-07-17 06:12:02       28 阅读
  3. linux中导出sql脚本

    2024-07-17 06:12:02       21 阅读
  4. git 提交远程仓库 方式

    2024-07-17 06:12:02       27 阅读
  5. 热修复的原理

    2024-07-17 06:12:02       22 阅读
  6. Springboot 3.x - Reactive programming (2)

    2024-07-17 06:12:02       25 阅读
  7. C++基础语法:STL之容器(1)--容器概述和序列概述

    2024-07-17 06:12:02       31 阅读
  8. 【前端】原生实现图片的放大与缩放

    2024-07-17 06:12:02       22 阅读
  9. Meta Llama - Model Cards & Prompt formats

    2024-07-17 06:12:02       22 阅读
  10. 后端开发面试题

    2024-07-17 06:12:02       22 阅读
  11. 自动化回滚的艺术:Conda包依赖的智能管理策略

    2024-07-17 06:12:02       26 阅读
  12. 探索Dubbo的服务引用:XML配置方式

    2024-07-17 06:12:02       26 阅读