python正则表达式

元字符 . ^ $ * + ? {} [] | () \

. 匹配除换号符以为的任意一个符号

ret=re.findall('李.','李杰,李刚,王超,占山,李莲英')
print(ret)
执行结果
['李杰', '李刚', '李莲']
ret2=re.findall('李..','李杰,李刚,王超,占山,李莲英')
print(ret2)
执行结果
['李杰,', '李刚,', '李莲英']

^ 以指定字符开头

ret3=re.findall('^李.','yuan李杰,李刚,王超,占山,李莲英')
print(ret3)
执行结果
[]

$ 以指定字符结尾

ret=re.findall('英$','yuan李杰,李刚,王超,占山,李莲英')
print(ret)
执行结果
['']

重复 *:[0,∞)   +:[1,∞)     ?:[0,1]     {}:指定次数

ret4=re.findall('\d{18}','2665622,54694433,4113261996035402554,5565599')
print(ret4)
执行结果
['411326199603540255']
ret5=re.findall('131\d*','13158956,6354895131696,6587952132254131')
print(ret5)
执行结果
['13158956', '131696', '131']

 \ 转义符,将元字符转换为普通符号;将一些普通符号转换为特殊功能的符号
\d匹配任何十进制数,等于[0-9]
\D匹配任何非数字字符,等于[^0-9]
\s匹配任何空白字符,等于[\t\n\r\f\v]
\S匹配任何非空白字符,等于[^\t\n\r\f\v]
\w匹配任何字母数字字符,等于[a-zA-Z0-9_]
\W匹配任何非字母数字字符,等于[^a-zA-Z0-9_]
\b匹配一个特殊字符边界,比如空格,&,#等

ret=re.findall(r'I\b','hello I am xiaobai')
print(ret)
执行结果
['I']
ret=re.findall('\d','hello I am xiaobai')
print(ret)
执行结果
[]
ret=re.findall('\D','hello I am xiaobai')
print(ret)
执行结果
['h', 'e', 'l', 'l', 'o', ' ', 'I', ' ', 'a', 'm', ' ', 'x', 'i', 'a', 'o', 'b', 'a', 'i']
ret=re.findall('\s','hello I am xiaobai')
print(ret)
执行结果
[' ', ' ', ' ']
ret=re.findall('\S','hello I am xiaobai')
print(ret)
执行结果
['h', 'e', 'l', 'l', 'o', 'I', 'a', 'm', 'x', 'i', 'a', 'o', 'b', 'a', 'i']
ret=re.findall('\w','hello I am xiaobai')
print(ret)
执行结果
['h', 'e', 'l', 'l', 'o', 'I', 'a', 'm', 'x', 'i', 'a', 'o', 'b', 'a', 'i']
ret=re.findall('\W','hello I am xiaobai')
print(ret)
执行结果
[' ', ' ', ' ']

 | 或者

ret=re.findall('www\.(?:\w+)\.(?:com|cn)','www.oldboy.com;www.oldboy.cn;www.baidu.com')
print(ret)
执行结果
['www.oldboy.com', 'www.oldboy.cn', 'www.baidu.com']

 

()分组 优先寻找并显示分组内容

ret6=re.findall('yuan+','fadsyuannnnndayuanyuanyaun')
print(ret6)
执行结果
['yuannnnn', 'yuan', 'yuan']
ret7=re.findall('(yuan)+','fadayuannnnasdayuanyuanyaun')
print(ret7)
执行结果
['yuan', 'yuan']
ret=re.search('\d|(ab)','rabhdg8sd')
print(ret.group())
执行结果
ab
ret=re.findall('\d|(?:ab)','rabhdg8sd')
print(ret)
执行结果
['ab', '8']

命名分组

ret10=re.search(r'-blog-articles-(?P<year>20[01]\d)-(?P<month>[01][1-9])','-blog-articles-2017-09')
print(ret10.group())
print(ret10.group('month'))
执行结果
-blog-articles-2017-09
09

字符集[ ],只匹配一个符号,元字符失效;只有三个特殊符号:- ^ \

ret=re.findall(r'a[^\d]c','dasda54adaga3casc')
print(ret)
执行结果
['asc']

re.search() 只匹配一项符合规则的元素

ret8=re.search('\d+','123qweqweq654eqw863')
print(ret8)
print(ret8.group())
执行结果
<_sre.SRE_Match object; span=(0, 3), match='123'>
123

re.match() 只匹配字符串开始的位置

ret9=re.match('\d+','448959dasdas4741')
print(ret9.group())
执行结果
448959

 

#configparser模块 用于文件的读与写操作
import configparser
cfp=configparser.ConfigParser()
cfp['DEFAULT']={'ServerAliveInterval':45,'Compression':'YES','CompressionLevel':9,'ForwardX11':'YES'\
                }
cfp['bitbucket.org']={'User':'hg'}
cfp['topsecret.server.com']={'port':5000123,'ForwardX11':'no'}
with open('cfp.ini')as f:
    cfp.write(f)
#操作文件 读与写操作
cfp=configparser.ConfigParser()
cfp.read('cfp.ini')
print(cfp.sections())
print(cfp.items('bitbucket.org'))
print(cfp.options('bitbucket.org'))
 
#subprocess模块
import subprocess
s=subprocess.Popen('dir',shell=True)#开启新的进程
s.wait()
print('ending')
s=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)
print(s.stdout.read().decode('gbk'))

 

posted @ 2017-06-26 16:22  吃柚子的小白  阅读(179)  评论(0编辑  收藏  举报