首页 > 编程语言 >Python模块-re模块实例

Python模块-re模块实例

时间:2023-08-07 17:35:21浏览次数:58  
标签:24 模块 04 Python re 2022 world findall

正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法。或者说:正则就是用来描述一类事物的规则。(在Python中)它内嵌在Python中,并通过 re 模块实现。

import re
  • \w与\W

    • \w匹配字母数字及下划线
    re.findall('\w','hello world 2022_04/24')
    ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '2', '0', '2', '2', '_', '0', '4', '2', '4']
    
    • \W匹配非字母数字下划线
    re.findall('\W','hello world 2022_04/22')
    [' ', ' ', '/']
    
  • \s与\S

    • \s匹配任意空白字符,等价于[\t,\n,\r,\f]
    re.findall('\s','hello world 2022_04/22')
    [' ', ' ']
    
    • \S匹配任意非空字符
    re.findall('\S','hello world 2022_04-24')
    ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '2', '0', '2', '2', '_', '0', '4', '-', '2', '4']
    
  • \d与\D

    • \d匹配任意数字
    re.findall('\d','hello world 2022_04-24')
    ['2', '0', '2', '2', '0', '4', '2', '4']
    
    • \D匹配任意非数字
    re.findall('\D','hello world 2022_04-24')
    ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', ' ', '_', '-']
    
  • \A与\Z

    • \A匹配字符串开始
    re.findall('\Ah','hello world 2022_04-24')
    ['h']
    
    • \Z匹配字符串结束,如果存在换行,只匹配到换行前的结束字符串
    re.findall('24\Z','hello world 2022_04-24')
    ['24']
    
  • ^与$等价于\A与\Z

    • ^
    re.findall('^he','hello world 2022_04-24')
    ['he']
    
    • $
    re.findall('24$','hello world 2022_04-24')
    ['24']
    
  • 重复匹配:| . | * | ? | .* | .*? | + | {n,m} |

    • .中间只匹配一个字符,可以为空
    re.findall('o.w','leeleeeeeleeo worleee  ')
    ['o w']
    
    • *左侧字符0或无数个
    re.findall('ll*','leeleeeeeleeo worleee  ')
    ['l', 'l', 'l', 'l']
    
    • ?左侧字符0个或1个
    re.findall('el?','leeleeeeeleeo worleee  ')
    ['e', 'el', 'e', 'e', 'e', 'e', 'el', 'e', 'e', 'e', 'e', 'e']
    
    • +左侧字符1个或无穷个
    re.findall('ll+','leeleeeeeleeo worleee  ')
    []
    
    • {n,m}左侧字符最少重复n次,最多m次
    re.findall('le{2,4}','leeleeeeeleeo worleee  ')
    ['lee', 'leeee', 'lee', 'leee']
    
    • .*默认全部匹配
    re.findall('l.*l','leeleeeeeleeo worleee  ')
    ['leeleeeeeleeo worl']
    
    • .*?
    re.findall('l.*?l','leeleeeeeleeo worleee  ')
    ['leel', 'leeo worl']
    

标签:24,模块,04,Python,re,2022,world,findall
From: https://www.cnblogs.com/yigehulu/p/17611816.html

相关文章

  • uniapp获取位置时显示getLocation:fail the api need to be declared in the required
    uniapp获取位置时显示getLocation:failtheapineedtobedeclaredintherequiredPrivateInfosfieldinapp.json/ext.json解决方式:1.manifest.json文件 "mp-weixin" 中添加"permission":{"scope.userLocation":{&quo......
  • 笔记|聚类分析基础《Python数学实验与建模》
    参考图书为:《Python数学实验与建模》司守奎,孙玺菁定义将相似元素聚为一类通常分为Q型聚类(样本聚类)、R型聚类(指标聚类)。数据变换\(A=\begin{pmatrix}a_{11}&a_{12}&a_{13}&\cdots&a_{1p}\\a_{21}&a_{22}&a_{23}&\cdots&a_{2p}\\a_{31}&a_{32}&a_{33}&\cdots&a......
  • Azure Terraform(十四)Azure Key Vault 的机密管理
    一,引言最近有网友私信我,将Terraform部署到 Azure是一种将基础结构作为代码进行管理的好方法,但是如何使用AzureKeyVault来存储我们的Secret?在这篇博文中,我将给大家展示一下展示如何使用 Terraform引用 AzureKeyVaultSecret。1)这个时候就有人问了,Secret信息......
  • js replace方法 (字典表匹配替换字符)
    常规replace使用consttext1='abcdefg'consttext2=text1.replace('bc','00')//text2='a00defg'字典表匹配替换constreplacements={'<':'<','>':'>',......
  • Atcoder Grand Contest 058 F - Authentic Tree DP
    考虑给\(f(T)\)赋予组合意义。一个直观的想法是,在每条边中间新建一个节点,然后每次选择一条边对应的点,然后把它删掉,递归剩余的两个部分,但是你会发现这样分母不对,应该是\(n\)但在这个模型里只有\(n-1\)。考虑魔改这个模型。我们在每个边对应的点下面添加\(998244352\)个点,你......
  • [Redis]Redis (2) 扩展数据结构: Bitmap
    redisbitmapjavaspringboot1Redis数据结构之bitmap#设置bitmap字符串指定位置的值|SETBITkeyoffsetvaluesetbitsingleSquare:recommend:userId:39991>>0#查看bitmap字符串的长度|占用字节数:=(max_offset/8)+1strlensingleSquare:recommend:us......
  • Linux:防火墙iptables与firewalld的启停
    Linux关闭防火墙firewall和iptables命令_永久关闭iptables防火墙_红烧柯基的博客-CSDN博客Linux防火墙——iptables以及firewalld的使用介绍_树下一少年的博客-CSDN博客干货!Linux防火墙配置(iptables和firewalld)_数据包_规则_进行 iptables与firewalld1、状态syste......
  • How to get User Name from External Login in ASP.NET Core?
    HowtogetUserNamefromExternalLogininASP.NETCore? 回答1DoyouhaveaccesstoSignInManagerorcanyouinjectit?Ifyes,thenthisishowyouwouldaccessuserid(username),email,first&lastname:publicclassMyController:Microsoft.Asp......
  • 11REST表述性状态转移
    REST是一种通常使用HTTP和XML进行基于WEB通信的技术,可以降低开发的复杂性,提高系统的可伸缩性以往的每一个命令对应一个接口,命令数量多导致接口爆炸,现在通过REST技术将不同的命令都可以通过相同的接口实现相应的功能。 REST的5个原则:网络上所有的事物都应该抽象为资源每个资......
  • Python&Swift 三元(目)运算
    Python的三元运算写法:is_true=Trueresult='TRUE'ifis_trueelse'FALSE'#output:TRUESwift的三元运算写法:isTrue=trueresult=isTrue?"TRUE":"FALSE"//output:TRUE......