Mind Map

Python RE

或到 这里 查看高清图

import re

pattern = ...

string = ...

regex = re.compile(pattern)

match = regex.search(string)

RE Syntax

{m,n}

{0,} is the same as *, {1,} is equivalent to +, and {0,1} is the same as ?.

$

Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline.

foo matches both ‘foo’ and ‘foobar’, while the regular expression foo$ matches only ‘foo’. More interestingly, searching for foo.$ in 'foo1\nfoo2\n' matches ‘foo2’ normally, but ‘foo1’ in MULTILINE mode; searching for a single $ in 'foo\n' will find two (empty) matches: one just before the newline, and one at the end of the string.

(?imsx-imsx:...)

(Zero or more letters from the set i, m, s, x, optionally followed by - followed by one or more letters from the same set.) The letters set or removes the corresponding flags: re.I (ignore case), re.M (multi-line), re.S (dot matches all), and re.X (verbose), for the part of the expression.

\A v.s. ^

When not in MULTILINE mode, \A and ^ are effectively the same. In MULTILINE mode, they’re different: \A still matches only at the beginning of the string, but ^ may match at any location inside the string that follows a newline character.

\Z v.s. $

\Z: Matches only at the end of the string.

$: Matches at the end of a line, which is defined as either the end of the string, or any location followed by a newline character.

\b

Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of word characters.Note that formally, \b is defined as the boundary between a \w and a \W character (or vice versa), or between \w and the beginning/end of the string. This means that r'\bfoo\b' matches 'foo', 'foo.', '(foo)', 'bar foo baz' but not 'foobar' or 'foo3'.

Word boundary. This is a zero-width assertion.They don’t cause the engine to advance through the string; instead, they consume no characters at all, and simply succeed or fail. For example, \b is an assertion that the current position is located at a word boundary; the position isn’t changed by the \b at all. This means that zero-width assertions should never be repeated, because if they match once at a given location, they can obviously be matched an infinite number of times.

RE: Module Contents

re.L(re.Locale)

Make \w, \W, \b, \B and case-insensitive matching dependent on the current locale. This flag can be used only with bytes patterns. The use of this flag is discouraged as the locale mechanism is very unreliable, it only handles one “culture” at a time, and it only works with 8-bit locales. Unicode matching is already enabled by default in Python 3 for Unicode (str) patterns, and it is able to handle different locales/languages. Corresponds to the inline flag (?L).

Locales are a feature of the C library intended to help in writing programs that take account of language differences. For example, if you’re processing encoded French text, you’d want to be able to write \w+ to match words, but \w only matches the character class [A-Za-z] in bytes patterns; it won’t match bytes corresponding to é or ç. If your system is configured properly and a French locale is selected, certain C functions will tell the program that the byte corresponding to é should also be considered a letter. Setting the LOCALE flag when compiling a regular expression will cause the resulting compiled object to use these C functions for \w; this is slower, but also enables \w+ to match French words as you’d expect.

read more

python re库常用_Python re库指南相关推荐

  1. python 魔法方法常用_Python魔法方法指南

    有很多人说学习Python基础之后不知道干什么,不管你是从w3c还是从廖雪峰的教程学习的,这些教程都有一个特点:只能引你快速入门,但是有关于Python的很多基础内容这些教程中都没介绍,而这些你没学习 ...

  2. python常用标准库有哪些-Python开发中常用的标准库

    大多数基于 Python 开发的应用程序都会用到本地标准库和三方库,这样不仅能让我们把时间去关注真正的业务开发,也能学习到更多价值含量高的程序设计和开发思想.程序开发中有一句著名的话叫做: Don't ...

  3. python安卓库拍照_Python Faker库的实战,用Faker库生成伪造的安卓通讯录

    老板的需求总是各种各样的,作为程序员就只能想办法实现.这不突然让帮他生成几个虚假的通讯录.具体需求是这样的:老板给我个电话簿,让我给这个电话簿添加上姓名,公司,邮箱之类的数据然后导入到安卓手机的通讯录 ...

  4. python pexpect linux安装_Python Pexpect库的使用

    简介 最近需要远程操作一个服务器并执行该服务器上的一个Python脚本,查到可以使用Pexpect这个库.记录一下. 什么是Pexpect?Pexpect能够产生子应用程序,并控制他们,并能够通过期望 ...

  5. qpython3h第三方库安装_Python第三方库安装

    Python有一个全球社区:https://pypi.org/,在这里我们可以搜索任何主题的Python第三方库.PyPI全称是Python Package Index,指的是Python包的索引,它 ...

  6. python中matplotlib库实例_Python Matplotlib库入门指南

    Matplotlib简介 Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形.最初是模仿了Matlab图 ...

  7. python与excel常用的第三方库_Python读写Excel文件第三方库汇总

    常见库简介 xlrd xlrd是一个从Excel文件读取数据和格式化信息的库,支持.xls以及.xlsx文件. http://xlrd.readthedocs.io/en/latest/ 1.xlrd ...

  8. python标准类型内建函数_Python 标准库系列之内建函数

    Python的强大有很大一部分是因为其庞大的标准库提供了各式各样的功能,基本上日常使用中你所需要的功能都可以从中找到,从而大大减轻开发人员的压力,节省开发人员的时间,因此,要想更好的使用python, ...

  9. python源码库安装_Python第三方库安装及常见问题

    源码安装 Python第三方库几乎都可以在github或者 pypi上找到源码.源码包格式大概有zip . tar.zip. tar.bz2.解压这些包,进入解压好的文件夹,通常会有一个setup.p ...

最新文章

  1. Android Stadio配置了gralde的本地路径,但是windos 命令行还是会下载gradle
  2. 【Groovy】Groovy 脚本调用 ( Groovy 脚本中调用另外一个 Groovy 脚本 | 绑定作用域 binding 变量分析 | Binding 类 variables 成员分析 )
  3. CSS Dock Menu:JS+CSS 仿苹果MAC机桌面导航菜单
  4. 单机 Oracle 11g(11.2.0.4)手动打补丁PSU(11.2.0.4.8)
  5. 《炉石传说》架构设计赏析(4):Asset管理
  6. 安装ORACLE 11.2.0.3 错误
  7. Linux内核分析06
  8. spingboot 集成swagger2
  9. LeetCode 1829. 每个查询的最大异或值(前缀异或 + 位运算)
  10. Python监控Windows下的文件变化
  11. C++正则表达式创建匿名函数
  12. oracle qmon,10g QMON Architecture及AQ_TM_PROCESSES
  13. C++ UPD广播异步发包工具
  14. scrum敏捷开发、敏捷测试,快速迭代实践
  15. 图扑软件以轻量化建模构建智慧城市
  16. 【北亚数据恢复】zfs文件系统的服务器误删除的数据恢复
  17. linux系统的超级管理员,系统的超级管理员:root《 Linux 文件与目录权限 》
  18. ubantu与CentOS虚拟机之间搭建GRE隧道
  19. 旁流综合水处理器的安装与连接技巧
  20. 处cp显示服务器开小差,我和影帝处CP最新章节叶扶予蔺洲叶扶予小说阅读

热门文章

  1. 两个列表之间移动数据
  2. 星际迷航3 -- 父亲的纪念
  3. 【物联网智能网关-05】扫描键盘编程设计
  4. 枚举参考Hurry Up(三分)
  5. 3745路由器配置简单的dhcp server
  6. Linux无盘教程,如何无盘启动Linux
  7. spring 源码 找不到 taskprovider_Spring 源码阅读环境的搭建
  8. html h 不换行,css 强制不换行
  9. linux开机启动遇到grub启动_Linux如何跳过grub启动
  10. python count函数时间复杂度_Python(算法)-时间复杂度和空间复杂度