Python(re模块,正则)
day18
正则表达式用处?
匹配 字符串
s = 'hello world'
print(s.find('llo'))#第一个的位置
ret = s.replace('ll','xx')
print(ret)
print(s.split(' '))
执行结果
2
hexxo world
['hello', 'world'] Process finished with exit code 0
以上字符串提供的是完全匹配
模糊匹配,以上方法不可行,需正则表达式
import re
ret = re.findall('w\w{2}l','hello world') # ['worl']
print(ret)
ret1 = re.findall('alex', 'aalexsbvesalexfwfawea')#完全匹配,但是没必要 ['alex', 'alex']
print(ret1)
print(' ')
#元字符
# . 代指所有符号,除换行符\n 通配符 .
reta = re.findall('w..l', r'hello w ld') #只能代指任意一个字符 ['w l']
print(reta)
print(' ')
# ^
retb = re.findall('^h...o', 'hjasdadsdhello') #只在开始匹配,尽管后面有hello []
print(retb)
print(' ')
# $
retc = re.findall('a..x$','asfsacsxjaoox') #只在最后匹配 ['aoox']
print(retc)
print(' ')
# * 重复匹配
retd = re.findall('al.*i', 'sanvjalexli') #[0,+oo] 匹配0到多次 ['alexli']
print(retd)
print(' ')
# + 重复匹配
rete = re.findall('ab+', 'sanvjah') #[1,+oo] 匹配1到多次,a后面必须接上b []
print(rete)
print(' ')
# ? [0,1]
retf = re.findall('a?b', 'aaabhghabfb') #0个或者1个a,后面接b ['ab', 'ab', 'b']
print(retf)
print(' ')
# {}
retg = re.findall('a{5}b', 'vaaaaaab') #5个a ['aaaaab']
print(retg)
reth = re.findall('a{1,3}b', 'vaab') #贪婪匹配,设定一个范围,按最多的取 ['aab']
print(reth)
执行结果
['worl']
['alex', 'alex'] ['w l'] [] ['aoox'] ['alexli'] [] ['ab', 'ab', 'b'] ['aaaaab']
['aab'] Process finished with exit code 0
结论:*等于{0,正无穷} +等于{1,正无穷} ?等于{0,1}
# 字符集
reta = re.findall('a[c,d]x', 'adx') # ['adx']
print(reta) retb = re.findall('[a-z]', 'adx') # a到z都可以匹配 ['a', 'd', 'x']
print(retb)#['a', 'd', 'x'] # [] 取消元字符的特殊功能, 但是\ ^ - 例外
retc = re.findall('[w,*]', 'wad*') # *为一个普通字符 ['w', '*']
print(retc) #但是\ ^ - 例外
retd = re.findall('[1-9,a-z,A-Z]', '12sfsA') # *为一个普通字符 ['1', '2', 's', 'f', 's', 'A']
print(retd) # ^放在[]中意味着去反
rete = re.findall('[^s,A]', '12sfsA') # 除了s和A ['1', '2', 'f']
print(rete) # \
# 反斜杠后面跟元字符去除特殊功能
# 反斜杠后面跟普通字符实现特殊功能 print(re.findall('\d{11}', 'scbdhsh3123124326543')) # \d数字[0-9] ['31231243265'] print(re.findall('\sasd', 'fak asd')) # \s匹配任何空白字符 [' asd'] print(re.findall('\w', 'scbhsh 12432'))# \w字母数字 ,相当于a-zA-Z0-9 ['s', 'c', 'b', 'h', 's', 'h', '1', '2', '4', '3', '2'] print(re.findall(r'I\b', 'hello, I am LI$T'))# \b匹配一个特殊字符的边界 ['I', 'I'] #匹配出第一个满足的结果
retf = re.search('sb', 'scbdsbhshsb')# 对象
print(retf) #<_sre.SRE_Match object; span=(4, 6), match='sb'>
print(retf.group()) retg = re.search('a\.g', 'a.gj').group()# 此时的点并不是通配符,只是一个 . a.g
print(retg)
执行结果:
['adx']
['a', 'd', 'x']
['w', '*']
['', '', 's', 'f', 's', 'A']
['', '', 'f']
['']
[' asd']
['s', 'c', 'b', 'h', 's', 'h', '', '', '', '', '']
['I', 'I']
<_sre.SRE_Match object; span=(4, 6), match='sb'>
sb
a.g Process finished with exit code 0
()
import re
ret = re.findall(r'\\d', 'abc\de')# r 代表原生字符串,将\\d传给re,而非python解释器
print(ret) m = re.search(r'\bblow', 'blow')
print(m) print(re.search('(as)+', 'adcdasasas').group())#asas as为一个整体,匹配多个as print(re.search('(as)|3', 'as3').group()) #或
执行结果
['\\d']
<_sre.SRE_Match object; span=(0, 4), match='blow'>
asasas
as Process finished with exit code 0
1 import re
2 ret = re.findall("www.(\w+).com", "www.baidu.com") #() 组的概念
3 print(ret)
执行结果:
['baidu'] Process finished with exit code 0
正则表达式的方法:
1.findall():所有结果都返回一个列表
2.search():返回匹配到的一个对象,对象可调用group()返回结果
3.match():只在字符串开始进行匹配
import re
eta = re.match('asd', 'fhdsasd')#只在字符串开始进行匹配 None
print(reta) retb = re.split('[j,s]', 'sdjksal')# 注意空字符 ['', 'd', 'k', 'al']
print(retb) retc = re.sub('a..x', 's.....b', 'ffghalexbn') # 替换 ffghs.....bbn
print(retc) retd = re.compile('\.com')#规则被编译到对象, 点.不再是通配符
rete = retd.findall('adcdscd.com')#对象中已经有规则
print(rete)
执行结果:
None
['', 'd', 'k', 'al']
ffghs.....bbn
['.com'] Process finished with exit code 0
参考:
http://www.cnblogs.com/yuanchenqi/articles/5732581.html
Python(re模块,正则)的更多相关文章
- python,re模块正则
python没有正则需要导入re模块调用.正则表达式是为了匹配字符串,动态模糊的匹配,只要有返回就匹配到了, 没返回就没匹配到,前面是格式后面是字符串 最常用的匹配语法: re.match()#麦驰, ...
- python自动化--模块操作之re、MySQL、Excel
一.python自有模块正则 import re # re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None print(re.match("www ...
- Python使用re模块正则式的预编译及pickle方案
项目上线要求当中有言论和昵称的过滤需求, 客户端使用的是python脚本, python脚本中直接利用re模块来进行正则匹配, 一开始的做法是开启游戏后, 每帧编译2条正则式, 无奈运营需求里面100 ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- Python全栈 正则表达式(re模块正则接口全方位详解)
re模块是Python的标准库模块 模块正则接口的整体模式 re.compile 返回regetx对象 finditer fullmatch match search 返回 match对象 match ...
- python——re模块(正则表达)
python——re模块(正则表达) 两个比较不错的正则帖子: http://blog.csdn.net/riba2534/article/details/54288552 http://blog.c ...
- Python之模块、正则
一.模块import 模块的实质就是把要导入模块里面的代码,从上到下执行一遍,找模块的顺序是,先从当前目录下找,找不到的话,再环境变量里面找导入的模块名字最好不要有.,a.import sysprin ...
- Day05 - Python 常用模块
1. 模块简介 模块就是一个保存了 Python 代码的文件.模块能定义函数,类和变量.模块里也能包含可执行的代码. 模块也是 Python 对象,具有随机的名字属性用来绑定或引用. 下例是个简单的模 ...
- python常用模块(1):collections模块和re模块(正则表达式详解)
从今天开始我们就要开始学习python的模块,今天先介绍两个常用模块collections和re模块.还有非常重要的正则表达式,今天学习的正则表达式需要记忆的东西非常多,希望大家可以认真记忆.按常理来 ...
随机推荐
- python 迭代器/生成器/迭代对象
生成器: 带有yield的函数 迭代器:带有next 方法的对象,可以作为内建函数next的参数 迭代对象:带有__iter__方法的对象,__iter__方法返回迭代器 -------------- ...
- MySqlDBHelper
代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...
- vbs执行系统命令
首先说明一下,我的所有代码都是vbscript,jscript我没有研究过,不过我想也差不多. 关于最基础的语法比如变量的申明,分支,循环,函数的调用,等等这些我就不讲了,不懂得自己看一下. 1.我们 ...
- Linux automake命令
1)automake 使用教程 http://loftor.com/archives/automake.html 2)configure.in Makefile.am解析 http://blog.cs ...
- android studio使用真机测试时点击Debug调试模式时报Error running app:No target device found,点击运行模式却是启动正常的
原因是adb没检测到设备(包括真机和虚拟机). 在Terminal执行adb devices命令,查看有没有连接到的设备. 如果没有设备,确认虚拟机是否正确打开,真机是否连接打开USB调试并安装驱动. ...
- 解决windows搭建jenkins执行selenium无法启动浏览器问题
因为jenkins是用windows installer 安装成windows的服务了,那么jenkins是一个后台服务,所以跑selium cases 的时候不显示浏览器 Step 1. Contr ...
- spark standalone集群部署 实践记录
本文记录了一次搭建spark-standalone模式集群的过程,我准备了3个虚拟机服务器,三个centos系统的虚拟机. 环境准备: -每台上安装java1.8 -以及scala2.11.x (x代 ...
- 看图说说JVM GC收集算法
- cenos 安装nginx并添加到service
系统平台:CentOS release 6.6 (Final) 64位. 一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtoo ...
- Android-FileIOUtils工具类
文件读写相关工具类 public final class FileIOUtils { private FileIOUtils() { throw new UnsupportedOperationExc ...