Python正则速查

2023-11-12

Python正则速查

特殊情况

‘\’: special characters
‘\\’: match a literal backslash

r’ ’ : Python’s raw string notation for regular expression patterns

‘\number’: ‘\x00’

Special characters:

• '.' : any character except a newline (DOTALL: newline)
• '^' : the start of the string (MULTILINE)
• '$' : the end of the string (MULTILINE)
• '*' : 0 or more repetitions of the preceding RE
• '+' : 1 or more repetitions of the preceding RE
• '?' : 0 or 1 repetitions of the preceding RE
• '*?', '+?', '??' : non-greedy
• {m} : specifies that exactly m copies of the previous RE should be matched
• {m, n} : match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible. {m, } and {,n} are useful
• {m, n}? : attempting to match as few repetitions as possible
• '/' : either escapes special characters, or signals a special sequence
• [] : used to indicate a set of characters
    ○ [amk] match 'a', 'm', 'k'
    ○ [a-z], [0-9], [0-9A-Fa-f]
    ○ Special characters lose their special meaning inside sets. [(*+)] match '(', ')', '+', '*'
    ○ Character classes like \w or \S are also accepted inside a set
    ○ [^5] match any character except '5'
    ○ [()[\]{}] and []()[{}] will both match a parenthesis
• '|' : A|B match either A or B (tried from left to right)
• (…) : matches whatever regular expression is inside the parentheses (can be retrieved after a match)
• (?...) : extension notation
    ○ (?iLmsux) : one or more letters from the set 'i', 'L', 'm', 's', 'u', 'x', the group matches the empty string
    ○ (?:…) : a non-capturing version of regular parentheses (the substring matched by the group cannot be retrieved after performing a match)
    ○ (?P<name>…) : each group name must be defined only once within a regular expression
    ○ (?P=name) : a backreference to a named group
    ○ (?#...) : a comment
    ○ (?=…) : matches if … matches next
    ○ (?!...) : matches if … doesn't match next
    ○ (?<=…) : a positive lookbehind assertion
    ○ (?<!...) : negative lookbehind assertion
    ○ (?(id/name)yes-pattern|no-pattern) : try to match with yes-pattern if the group with given id or name exists, and with no-pattern if it doesn't
• \number :  matches the contents of the group of the same number
• \A : matches only at the start of the string
• \b : matches the empty string, but only at the beginning or end of a word
• \B : matches the empty string, but only when it is not at the beginning or end of a word
• \d : decimal digit
• \D : non-digit character
• \s : any whitespace character
• \S : any non-whitespace character
• \w : alphanumeric character and the underscore
• \W : any non-alphanumeric character
• \Z : match only at the end of the string

Module contents

• re.compile(pattern, flags=0): compile a regular expression pattern, prepare for search and match
    ○ re.I : ignore case
    ○ re.L : locale dependent
    ○ re.M : Multi-line
    ○ re.S : dot matches all
    ○ re.U : Unicode dependent
    ○ re.X : verbose
    ○ re.DEBUG
• re.search(): look for the first location
• re.match(): at the beginning of string
• re.split(): split string
• re.findall(): all non-overlapping matches of pattern in string
• re.finditer(): return an iterator
• re.sub(pattern, rep1): replace the leftmost non-overlapping occurrences of pattern in string by the replacement rep1.
• re.subn(): return a tuple
• re.escape(): return a string with all non-alphanumerics backslashed
• re.purge(): clear the regualr expression cache
• re.error
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python正则速查 的相关文章

随机推荐

  • 判断字符串是否是正确的IP格式的C语言函数

    来自 http blog csdn net shanzhizi 一个用于识别字符串是否是IPV4的C语言函数 保留下来供大家参考使用 include
  • Unity-C#中关于时间戳的一些方法

    转自 http www narkii com club thread 367980 1 html
  • 【FPGA】RGMII接口

    目录 1 RGMII 接口概要 2 RGMII 接口介绍 2 1 MII接口 2 2 RMII接口 2 3 GMII接口 2 4 RGMII接口 1 RGMII 接口概要 以太网的通信离不开物理层 PHY 芯片的支持 以太网 MAC 和 P
  • linux中printf命令,总结linux下printf命令的用法

    printf format and print date 通过printf的选项格式化输出数据 基本英文学习 二进制 binanry number 八进制 otcal number 十进制 decimal number 十六进制 hexad
  • Codeforces Round #697 (Div. 3) C. Ball in Berland(1400)

    Codeforces 1475 C Ball in Berland 题目分析 这个题其实就是给你一堆坐标 让你找到合适的有多少对 思路分析 坐标的话 首先想到用 pair
  • SPSS语法的使用

    SPSS语法的使用 CDA数据分析师官网
  • drf小练习2

    目录 1 使用GenericAPIView写出book的5个接口 2 使用面向对象 写5个父类 继承GenericAPIView 某几个父类后 就有某几个接口 方法一 方法二 九个子类视图 方式一 方式二 1 使用GenericAPIVie
  • 图的表示形式:邻接矩阵和邻接表

    图是对象和对象之间存在的关系的有限集合 如果将对象表示为顶点 或节点 将关系表示为边 则可以得到以下两种图形 有向图 在有向图中 一条边由一对有序的顶点 i j 表示 其中边起源于顶点i并终止于顶点j 下面给出的是有向图的示例 图 D 1
  • 数学建模论文

    论文 一 首页 1 标题 2 摘要 2 1 摘要的开头段 2 2摘要的中间段 2 3 摘要的结尾段 2 4 摘要中常见的废话 一 问题重述 1 问题背景 1 2 问题提出 二 问题分析 三 模型假设 四 符号说明 五 模型的建立与求解 六
  • JavaScript设计模式——工厂模式

    作者 Hanpeng Chen 公众号 前端极客技术 文章首发个人博客 JavaScript设计模式 工厂模式 代码视界 在瞬息万变的前端领域 技术更新迭代非常快 我们经常能在网络上看到诸如 学不动了 之类的言论 但是作为一名前端开发工程师
  • oracle实例名,服务名等概念区别与联系

    数据库名 实例名 数据库域名 全局数据库名 服务名 这是几个令很多初学者容易混淆的概念 相信很多初学者都与我一样被标题上这些个概念搞得一头雾水 我们现在就来把它们弄个明白 一 数据库名 什么是数据库名 数据库名就是一个数据库的标识 就像人的
  • HTML+css网页设计

    HTML 超文本标记语言 和CSS 层叠样式表 是用于网页设计的关键技术 下面是一个简单的示例 展示如何使用HTML和CSS创建一个基本的网页设计 首先 我们需要创建一个HTML文件 并使用标签将内容包裹起来 在标签中 我们可以添加一些元数
  • org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request

    前段时间在开发Spring Boot项目时 遇到了一个很烦人的错误 提示为 org springframework web multipart MultipartException Failed to parse multipart ser
  • 测试人的职场之路,进阶提升?测试老鸟经验分享...

    目录 导读 前言 一 Python编程入门到精通 二 接口自动化项目实战 三 Web自动化项目实战 四 App自动化项目实战 五 一线大厂简历 六 测试开发DevOps体系 七 常用自动化测试工具 八 JMeter性能测试 九 总结 尾部小
  • linux 系统 ES开源工具 elastichd 的使用

    要求安装了 ES和 docker 执行命令 docker run p 9800 9800 d containerize elastichd 然后访问 主机名 9800 即可 打开之后是这样一个界面
  • Win10子系统Ubuntu的WSL2开启、图形界面安装以及CUDA配置

    文章目录 一 Win10开启Linux子系统 操作步骤 1 启用适用于 Linux 的 Windows 子系统 2 检查运行 WSL 2 的要求 3 启用虚拟机功能 4 下载 Linux 内核更新包 5 将 WSL 2 设置为默认版本 6
  • 使用公网IP的ubuntu移植QT运行用QT编写的服务器

    下面是ubuntu下移植Qt 将编好的可执行程序放在另一台主机 然后将Qt的库转移到这台主机上来 设置环境变量后 重启 可以运行qt可执行程序 export LD LIBRARY PATH opt Qt lib LD LIBRARY PAT
  • Extjs 双击关闭tab页

    var tab new Ext TabPanel region center deferredRender false activeTab 0 resizeTabs true turn on tab resizing minTabWidth
  • vue番茄钟 & electron打包

    序 平时对自己学习工作计划安排可以使用番茄钟去规划 番茄钟 一个很简单的时间管理方法 设置一个固定时间 根据自己情况调整 这个时间是一个倒计时 在这段时间内认真去做一件事情 然后一个番茄钟结束后 休息大概五分钟 重新番茄钟 这可以帮助我们量
  • Python正则速查

    Python正则速查 特殊情况 special characters match a literal backslash r Python s raw string notation for regular expression patte