0.

1.参考

Python正则表达式指南

https://docs.python.org/2/library/re.html

https://docs.python.org/2/howto/regex.html

https://docs.python.org/3/library/re.html

string re 备注
  re.match(pattern, string, flags=0) at the start of the string
S.find(sub [,start [,end]]) -> int re.search(pattern, string, flags=0) Scan through string looking for a match
S.replace(old, new[, count]) -> string re.findall(pattern, string, flags=0) re.finditer

2.分组 m.group()

xx

In [560]: m.group?
Docstring:
group([group1, ...]) -> str or tuple.
Return subgroup(s) of the match by indices or names.
For 0 returns the entire match.
Type: builtin_function_or_method In [542]: m=re.search(r'(-{1,2}(gr))','pro---gram-files') In [543]: m.group() #自带
Out[543]: '--gr' In [544]: m.group(0) #自带,返回整个匹配到的字符串 For 0 returns the entire match. 注意 m.string 是被检索的完整原文。。。
Out[544]: '--gr' In [545]: m.group(1)
Out[545]: '--gr' In [546]: m.group(2)
Out[546]: 'gr' In [547]: m.group(3) #加的 ( 不满足则报错
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-547-71a2c7935517> in <module>()
----> 1 m.group(3) IndexError: no such group In [548]: m.group(1,2) #选择多个分组,返回tuple
Out[548]: ('--gr', 'gr') In [549]: m.groups() #选择所有分组
Out[549]: ('--gr', 'gr')

m.groupdict 用于命名分组

In [557]: m.groupdict?
Docstring:
groupdict([default=None]) -> dict.
Return a dictionary containing all the named subgroups of the match,
keyed by the subgroup name. The default argument is used for groups
that did not participate in the match
Type: builtin_function_or_method In [558]: m=re.search(r'(-{1,2}(?P<GR>gr))','pro---gram-files') In [559]: m.groupdict()
Out[559]: {'GR': 'gr'}

3.提取 re.findall()

re.findall(pattern, string, flags=0)

In [97]: text = "He was carefully disguised but captured quickly by police."

In [98]: re.findall(r"\w+ly", text)  #相当于 m.group(0)
Out[98]: ['carefully', 'quickly'] In [99]: re.findall(r"(\w+)ly", text) #手动加单个括号限定内容,相当于返回 m.group(1)
Out[99]: ['careful', 'quick'] In [100]: re.findall(r"((\w+)(ly))", text) #多个括号,从左到右数 (,相当于返回 m.groups()
Out[100]: [('carefully', 'careful', 'ly'), ('quickly', 'quick', 'ly')]

In [102]: re.findall(r"((1\w+)(ly))", text)
  Out[102]: []

4.替换 re.sub()

re.sub(pattern, repl, string, count=0, flags=0)

repl 里面的 前向引用 Backreferences, such as \6, are replaced with the substring matched by group 6 in the pattern. 也可以通过 func 实现。

注意 mysql regexp 不支持 \1

https://stackoverflow.com/questions/4122393/negative-backreferences-in-mysql-regexp  提到 unless you can install/use LIB_MYSQLUDF_PREG.

https://stackoverflow.com/questions/7058209/reference-to-groups-in-a-mysql-regex

In [158]: def func(m):
...: return m.group('DEF')+' '+m.group(2) #别名
...: In [159]: re.sub(r'(?P<DEF>def)\s+([a-z]+)\s*\(\s*\):', func, 'def func(): def f():')
Out[159]: 'def func def f' In [160]: re.sub(r'(?P<DEF>def)\s+([a-z]+)\s*\(\s*\):', r'\1 \2', 'def func(): def f():') #不支持 \别名
Out[160]: 'def func def f'

5. Backreferences 前向引用在 pattern

5.1扑克牌找对子

In [204]: re.search(r'(.).*\1','ab123')

In [205]: re.search(r'(.).*\1','ab121')
Out[205]: <_sre.SRE_Match at 0x71ca120> In [206]: _.group()
Out[206]: ''

5.2连续多个相同

In [207]: re.search(r'.{3}','')  #错误
Out[207]: <_sre.SRE_Match at 0x71b94a8> In [208]: re.search(r'(.){3}','') #错误
Out[208]: <_sre.SRE_Match at 0x71ca198> In [209]: re.search(r'(.)\1\1','') #正确 In [210]: re.search(r'(.)\1\1','')
Out[210]: <_sre.SRE_Match at 0x71ca210> In [211]: re.search(r'(.)\1{2}','')
Out[211]: <_sre.SRE_Match at 0x71ca288> In [212]: _.group()
Out[212]: ''

python之re正则简单够用的更多相关文章

  1. Python正则简单实例分析

    Python正则简单实例分析 本文实例讲述了Python正则简单用法.分享给大家供大家参考,具体如下: 悄悄打入公司内部UED的一个Python爱好者小众群,前两天一位牛人发了条消息: 小的测试题:  ...

  2. python浅谈正则的常用方法

    python浅谈正则的常用方法覆盖范围70%以上 上一次很多朋友写文字屏蔽说到要用正则表达,其实不是我不想用(我正则用得不是很多,看过我之前爬虫的都知道,我直接用BeautifulSoup的网页标签去 ...

  3. python匹配ip正则

    python匹配ip正则 #!/usr/bin/env python # -*- coding:utf-8 -*- import re ip_str = "asdad1.1.1.1sdfwe ...

  4. python 多线程就这么简单(续)

    之前讲了多线程的一篇博客,感觉讲的意犹未尽,其实,多线程非常有意思.因为我们在使用电脑的过程中无时无刻都在多进程和多线程.我们可以接着之前的例子继续讲.请先看我的上一篇博客. python 多线程就这 ...

  5. python模块介绍- HTMLParser 简单的HTML和XHTML解析器

    python模块介绍- HTMLParser 简单的HTML和XHTML解析器 2013-09-11 磁针石 #承接软件自动化实施与培训等gtalk:ouyangchongwu#gmail.comqq ...

  6. 基于Python使用SVM识别简单的字符验证码的完整代码开源分享

    关键字:Python,SVM,字符验证码,机器学习,验证码识别 1   概述 基于Python使用SVM识别简单的验证字符串的完整代码开源分享. 因为目前有了更厉害的新技术来解决这类问题了,但是本文作 ...

  7. Python的变量及简单数据类型

    Python的变量及简单类型 1.  变量 在Python编程中,变量是用来存放值或对像的容器.变量的名称可以自定义,但需遵循一定的规范,否则可能会引发一些错误.Python的变量可以分为数字.字符和 ...

  8. Python与C的简单比较(Python3.0)

    Python可以说是目前最火的语言之一了,人工智能的兴起让Python一夜之间变得家喻户晓,Python号称目前最最简单易学的语言,现在有不少高校开始将Python作为大一新生的入门语言.本萌新也刚开 ...

  9. Python 基于Python及zookeeper实现简单分布式任务调度系统设计思路及核心代码实现

    基于Python及zookeeper实现简单分布式任务调度系统设计思路及核心代码实现   by:授客 QQ:1033553122 测试环境 功能需求 实现思路 代码实践(关键技术点实现) 代码模块组织 ...

随机推荐

  1. HTTP简明学习

    前面的话 本文将详细介绍HTTP主要内容 概述 Web 的诞生,源于三大技术的诞生,它们都是当年 Web 之父 Tim Berners-Lee 自己 开发的,世界上第一个网站诞生的时间是 1991 年 ...

  2. 【Machine Translation】CMU的NMT教程论文:最全面的神经机器翻译学习教程

    这是一篇CMU发的神经机器翻译教程论文,很全很详细,适合新手阅读,即使没有什么MT.DNN.RNN的基础知识. 另外它还配套了CMU自己的一个框架DyNet的练习. 全文共9章,从统计语言模型到DNN ...

  3. 【NLP】Conditional Language Modeling with Attention

    Review: Conditional LMs Note that, in the Encoder part, we reverse the input to the ‘RNN’ and it per ...

  4. ElasticSsarch汇总

    用途: 分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索: 实时分析的分布式搜索引擎: 可以扩展到上百台服务器,处理PB级别的结构化或非结构化数据. 点击查看安装.基本增删改查操作REST ...

  5. Jupyter Notebook(推荐使用Anaconda安装)

    一.Jupyter Notebook介绍 1.简介 Jupyter Notebook是基于网页的用于交互计算的应用程序.其可被应用于全过程计算:开发.文档编写.运行代码和展示结果. 简而言之,Jupy ...

  6. python之旅九【第九篇】socket

    什么是socket 建立网络通信连接至少要一对端口号(socket).socket本质是编程接口(API),对TCP/IP的封装,TCP/IP也要提供可供程序员做网络开发所用的接口,这就是Socket ...

  7. [SCOI2009] 迷路

    题目类型:拆点, 矩阵快速幂 转化为矩阵快速幂,好题! 传送门:>Here< 题意:给出邻接矩阵,求\(1\)到\(N\)恰好长度为\(T\)的路径方案数 解题思路 如果题目给出的是一个\ ...

  8. Codeforces 1077E Thematic Contests(二分)

    题目链接:Thematic Contests 题意:给出n道有类型的题目,每天组织一场专题比赛,该天题目数量必须是前一天的2倍,第一天的题目数量可以任意选择,求能消耗的最多题目数量. 题解:先整理成不 ...

  9. CF809E Surprise me!(莫比乌斯反演+Dp(乱搞?))

    题目大意: 给你一棵树,树上的点编号为\(1-n\).选两个点\(i.j\),能得到的得分是\(\phi(a_i*a_j)*dis(i,j)\),其中\(dis(i,j)\)表示\(a\)到\(b\) ...

  10. python 学习地址

    本章主要记录学习python过程中借鉴的一些网站,并感谢这些博主辛勤付出. python官网:https://www.python.org/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...