一, 什么是正则?

  正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.

  在python中,正则内嵌在python中,并通过re模块实现,正则表达模式被编译成一系列的字节码,然后由c编写的匹配引擎执行.

二,常用的匹配模式(元字符)

import re

# 将所有的数据都找出来放进列表中list,一一匹配
print(re.findall('alex', 'haha alex is alex is dsb'))
# >>>: ['alex', 'alex'] # \w 匹配字母数字及下划线(一个\w每次匹配一个字符)
# \W 匹配非字母数字及下划线
print(re.findall('\w','Aah123 +-_'))
# >>>: ['A', 'a', 'h', '1', '2', '3', '_'] print(re.findall('\w\w','Aah123 +-_'))
# >>>: ['Aa', 'h1', '23'] print(re.findall('\w9\w','Aa9h123 aaa9c+-_'))
# >>>: ['a9h', 'a9c'] # \s 匹配任意空白字符,等价于\t\n\r\f
# \S 匹配非空字符 # \d 匹配任意数字,[0-9]
# \D 匹配任意非数字 # ^ : 仅从开头开始匹配
# $ : 仅从尾部开始匹配
print(re.findall('^alex', 'alex is alex is alex'))
# >>>: ['alex']
print(re.findall('^alex', '1alex is alex is alex'))
# >>>: []

重复匹配:  | . | * | ? | .* | .*? | + | {n,m}

# . :代表一个字符,该字符可以是任意字符(除换行符)
print(re.findall('a.c', 'a alc aaac a c asfdsaf'))
# >>>: ['alc', 'aac', 'a c'] print(re.findall('a.c', 'a alc aaac a\nc asfd',re.DOTALL)) #DOTALL使得.匹配包括换行符在内的所有字符
# >>>: ['alc', 'aac', 'a\nc']
# ? :代表左边那一个字符出现0次或者1次
print(re.findall('ab?', 'a ab abb abbbb a123b a123bbbb'))
# >>>: ['a', 'ab', 'ab', 'ab', 'a', 'a']
# * :代表左边那一个字符出现0次到无穷次
print(re.findall('ab*', 'a ab abb abbbb a123b a123bbbb'))
# >>>: ['a', 'ab', 'abb', 'abbbb', 'a', 'a']
# + :代表左边那一个字符出现1次到无穷次
print(re.findall('ab+', 'a ab abb abbbb a123b a123bbbb'))
# >>>: ['ab', 'abb', 'abbbb']
# {n,m} :代表左边那一个字符出现n次到m次
print(re.findall('ab{1,3}', 'a ab abb abbbb a123b a123bbbb'))
# >>>: ['ab', 'abb', 'abbb']
# .* :匹配任意0个到无穷个字符,贪婪匹配
print(re.findall('a.*c','a132142qwdcavcccc(((()))))c2333'))
# >>>: ['a132142qwdcavcccc(((()))))c']
# .*? :匹配任意0个到无穷个字符,非贪婪匹配
print(re.findall('a.*?c', 'a132142qwdcavcccc(((()))))c2333'))
# >>>: ['a132142qwdc', 'avc']
# |:或者
print(re.findall('companies|company', 'Too many companies have gone bankrupt,c and the next one is my company'))
# >>>: ['companies', 'company']
# ():分组
print(re.findall('compan(?:ies|y)','Too many companies have gone bankrupt,c and the next one is my company'))
# >>>: ['companies', 'company']
# \ :转义
print(re.findall('a\\\\c','a\c aac'))
print(re.findall(r'a\\c','a\c aac'))
# >>>: ['a\\c']
# 忽略大小写
# print(re.findall('alex','my name is alex ALex is dSB',re.I))
# # >>>: ['alex', 'ALex'] # msg = '''my name is egon
# asdfassg egon
# 122324324egon'''
# print(re.findall('egon$',msg,re.M))
# >>>: ['egon', 'egon', 'egon']
# []: 代表匹配一个字符,这个字符是来自于自定义的范围
print(re.findall('a[1]c', 'a a1c aaac a c asfdsaf',re.DOTALL))
# >>>: ['a1c']
print(re.findall('a[0-9]c', 'a a1c aaac a7c asfdsaf',re.DOTALL)) #[0-9]的数字
# >>>: ['a1c', 'a7c']
print(re.findall('a[a-zA-Z]c', 'a a1c aaac a7c asfdsaf',re.DOTALL)) #所有字母
# >>>: ['aac']
print(re.findall('a[+*/-]c', 'a a1c aaac a7c asfdsaf',re.DOTALL)) #-代表连字符,在首尾才表示符号意思
# re模块其他方法
res=re.findall('(href)="(.*?)"','<p>动感视频</p><a href="https://www.douniwan.com/1.mp4">逗你玩呢</a><a href="https://www.xxx.com/2.mp4">葫芦娃</a>')
print(res) res=re.search('(href)="(.*?)"','<p>动感视频</p><a href="https://www.douniwan.com/1.mp4">逗你玩呢</a><a href="https://www.xxx.com/2.mp4">葫芦娃</a>')
print(res)
print(res.group(0))
print(res.group(1))
print(res.group(2)) #运行结果
[('href', 'https://www.douniwan.com/1.mp4'), ('href', 'https://www.xxx.com/2.mp4')]
<_sre.SRE_Match object; span=(14, 51), match='href="https://www.douniwan.com/1.mp4"'>
href="https://www.douniwan.com/1.mp4"
href
https://www.douniwan.com/1.mp4

re模块(正则)的更多相关文章

  1. Python全栈 正则表达式(re模块正则接口全方位详解)

    re模块是Python的标准库模块 模块正则接口的整体模式 re.compile 返回regetx对象 finditer fullmatch match search 返回 match对象 match ...

  2. Python使用re模块正则式的预编译及pickle方案

    项目上线要求当中有言论和昵称的过滤需求, 客户端使用的是python脚本, python脚本中直接利用re模块来进行正则匹配, 一开始的做法是开启游戏后, 每帧编译2条正则式, 无奈运营需求里面100 ...

  3. python,re模块正则

    python没有正则需要导入re模块调用.正则表达式是为了匹配字符串,动态模糊的匹配,只要有返回就匹配到了, 没返回就没匹配到,前面是格式后面是字符串 最常用的匹配语法: re.match()#麦驰, ...

  4. 反射,hashlib模块,正则匹配,冒泡,选择,插入排序

    一.反射(自省) 首先通过一个例子来看一下本文中可能用到的对象和相关概念. import sys # 模块,sys指向这个模块对象import inspectdef foo(): pass # 函数, ...

  5. 常用模块-正则re

    常用模块之正则模块 """ 正则表达式与re模块的关系 1.正则表达式是一门独立的技术,任何语言均可使用 2.python中要想使用正则表达式需要通过re模块 " ...

  6. python3 re模块正则匹配字符串中的时间信息

    匹配时间: # -*- coding:utf-8 -*- import re def parseDate(l): patternForTime = r'(\d{4}[\D]\d{1,2}[\D]\d{ ...

  7. day19 python之re模块正则练习

    1.匹配标签 import re ret = re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>" ...

  8. Python(re模块,正则)

    day18 正则表达式用处? 匹配 字符串 s = 'hello world' print(s.find('llo'))#第一个的位置 ret = s.replace('ll','xx') print ...

  9. re模块与正则

    一.正则 正则就是用来筛选字符串中的特定的内容 正则表达式与re模块的关系: 1.正则表达式是一门独立的技术,任何语言都可以使用 2.python中药想使用正则表达式需要通过调用re模块 正则应用场景 ...

随机推荐

  1. 项目:《ssh框架综合项目开发视频》-视频目录和第六天的EasyUI简单讲解

    4 练习使用技术: Struts2 + hibernate5.x + spring4.x + mysql数据库 1 crm:customer relational manager,客户关系管理 2 c ...

  2. 同步&异步+阻塞&非阻塞(理解)

    0 - 同步&异步 同步和异步关注的是消息通信机制. 0.1 - 同步 由“调用者”主动等待这个“调用”结果.即是,发出一个“调用”时,在没有得到结果之前,该“调用”不返回,一旦调用返回,则得 ...

  3. okhttp3与旧版本okhttp的区别分析

    https://www.jianshu.com/p/4a8c94b239b4  待总结学习

  4. LR的损失函数&为何使用-log损失函数而非平方损失函数

    https://blog.csdn.net/zrh_CSDN/article/details/80934278 Logistic回归的极大似然估计求解参数的推导: https://blog.csdn. ...

  5. 阿里云IoT物联网平台入门教程

    参考链接:https://www.geek-workshop.com/thread-37883-1-1.html

  6. 20165221 2017-2018-2《Java程序设计》课程总结

    20165221 2017-2018-2<Java程序设计>课程总结 一.作业连接汇总 每周作业链接 预备作业一: 20165221我期望的师生关系 预备作业二: 20165221学习基础 ...

  7. 第三节,CNN案例-mnist手写数字识别

    卷积:神经网络不再是对每个像素做处理,而是对一小块区域的处理,这种做法加强了图像信息的连续性,使得神经网络看到的是一个图像,而非一个点,同时也加深了神经网络对图像的理解,卷积神经网络有一个批量过滤器, ...

  8. 异常:已引发: "设置 connectionId 时引发了异常。" (System.Xaml.XamlObjectWriterException) 引发了一个 System.Xaml.XamlObjectWriterException: "

    项目中,引用一个富文本编辑器,SmithHtmlEditor,进入页面的时候异常. 在View和ViewModel所在的类库引用. 还需要在Main中引用.

  9. 在Linux环境下使用Jexus部署ASP.NET Core

    关于如何在Linux中添加ASP.NET Core运行时环境请参考我的上一篇文章,本文章将不再做赘述. 本文章运行环境如下:  (1) 安装独立版Jexus 本教程安装的是独立版的Jexus,独立版的 ...

  10. init级别

    新年第一天从温习记模糊的旧知识开始: init级别包含0-6: 0:关机 1:单用户(Root Only) 2:多用户(不包含Net File System 3:多用户(完全) 4:安全模式 5:桌面 ...