Created
January 7, 2019 09:40
-
-
Save caoya171193579/dee7993e0ec9ccece9e850cd07176a1a to your computer and use it in GitHub Desktop.
进阶
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
使用正则表达式: | |
re.findall(规则名,匹配字符串名) :规则匹配参数 | |
re.compile() :把正则表达式编译成对象,匹配的速度要比规则块很多 | |
>>> import re | |
>>> | |
>>> c1 = r"\d{3,4}-?\d{8}$" #定义一个正则c1为前面最少三位数最多四位数-?后面八位数结尾的座机号码,?前面的-可有可无的规则。 | |
>>> | |
>>> c1 | |
'\\d{3,4}-?\\d{8}$' | |
>>> re.findall(c1,'0577-88667755') | |
['0577-88667755'] | |
>>> re.findall(c1,'0577-886677559') | |
[] | |
>>> re.findall(c1,'0577-8866775') | |
[] | |
>>> re.findall(c1,'05-8866775') | |
[] | |
>>> re.findall(c1,'057-88667755') | |
['057-88667755'] | |
>>> | |
############################################## | |
>>> caoya_tel=re.compile(c1) #将正则c1,编译为对象caoya_tel(tel:常指电话号码) | |
>>> | |
>>> caoya_tel | |
<_sre.SRE_Pattern object at 0x7f5c617b52e8> | |
>>> caoya_tel.findall('0377-12345678') #编译过后更快捷的来匹配规则。 | |
['0377-12345678'] | |
>>> caoya_tel.findall('037712345678') | |
['037712345678'] | |
>>> caoya_tel.findall('03771234567') | |
['03771234567'] | |
>>> caoya_tel.findall('0377-1234567') | |
[] | |
>>> caoya_tel.findall('03-1234567') | |
[] | |
>>> | |
############################################### | |
(re.I : 在匹配中不区分大小写的参数) | |
>>> caoya_name = re.compile(r"caoya",re.I) #在后面加上re.I 表示匹配的时候不区分大小写。 | |
>>> | |
>>> caoya_name.findall('CAOYA') | |
['CAOYA'] | |
>>> caoya_name.findall('CAOya') | |
['CAOya'] | |
>>> caoya_name.findall('caoya') | |
['caoya'] | |
>>> | |
re模块提供了一个正则表达式引擎的接口,可以将REstring(字符串)编译成对象并用它们来进行匹配 | |
re 所定义的 flag 包括: | |
re.I 忽略大小写 | |
re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境 | |
re.M 多行模式 | |
re.S 即为’ . ’并且包括换行符在内的任意字符(’ . ’不包括换行符) | |
re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库 | |
re.X 为了增加可读性,忽略空格和’ # ’后面的注释 | |
\: 反斜杠的麻烦。 在定义规则字符串前面加“r”,反斜杠就不会被任何特殊方式处理。section(塞克什):部分 | |
>>> caoya_name = "\asdfg" | |
>>> | |
>>> caoya_name | |
'\x07sdfg' | |
>>> re.findall(caoya_name,"asdfg") | |
[] | |
>>> re.findall(caoya_name,"\asdfg") | |
['\x07sdfg'] | |
>>> cao = "\\asd" | |
>>> | |
>>> cao | |
'\\asd' | |
>>> re.findall(cao,"asd") | |
[] | |
>>> re.findall(cao,"\asd") | |
['\x07sd'] | |
>>> re.findall(cao,"\\asd") | |
[] | |
>>> cao = "\\\\asd" | |
>>> cao | |
'\\\\asd' | |
>>> re.findall(cao,"\\asd") | |
['\\asd'] | |
>>> re.findall(cao,"\\\asd") | |
[] | |
>>> re.findall(cao,"\\\\asd") | |
['\\asd'] | |
>>> re.findall(cao,"\\\\as") | |
[] | |
>>> cao = r"\\\\asd" | |
>>> re.findall(cao,"\\\\asd") | |
['\\\\asd'] | |
>>> | |
#################################### | |
执行匹配:正则编译的对象,快捷用法。 | |
“RegexObject”实例有一些方法和属性,完整的列表可查阅 Python Library Reference | |
方法/属性 和作用: | |
match() : 决定RE正则是否在字符串刚开始的位置 | |
>>> import re | |
>>> | |
>>> caoya_re = re.compile(r'caoya',re.I) | |
>>> | |
#match() :马驰 参数,只匹配开头是caoya的字符串集,如果匹配就会返回对应的对象, object 对象的意思。 0x7f91947cc5e0 为对象值 | |
>>> caoya_re.match('caoya hello') | |
<_sre.SRE_Match object at 0x7f91947cc5e0> | |
>>> caoya_re.match('hello') #不匹配或者字符串开头的位置不对,则返回空白 | |
>>> caoya_re.match('hello caoya') | |
>>> caoya_re.match('caoya') | |
<_sre.SRE_Match object at 0x7f91947cc648> | |
>>> | |
search() : 扫描字符串,找到这个RE匹配的位置 | |
>>> caoya_re = re.compile(r'caoya',re.I) #re.I 不分大小写。 | |
>>> | |
#search 瑟驰 参数,匹配只要存在正则的规则的字符串不管它在任何位置,都可以,匹配,然后返回正则的对象, (‘object’:啊不捷克特 ,对象的意思) | |
>>> caoya_re.search('caoya') | |
<_sre.SRE_Match object at 0x7f91947cc5e0> | |
>>> caoya_re.search('nihao caoya') | |
<_sre.SRE_Match object at 0x7f91947cc648> | |
>>> caoya_re.search('nihao caoya hello') | |
<_sre.SRE_Match object at 0x7f91947cc5e0> | |
>>> caoya_re.search('nihao hello') | |
>>> | |
findall() : 找到RE匹配的所有子串,并把它们作为一个列表返回 | |
#不多说前面用了很多。 | |
>>> caoya_re = r'caoya' | |
>>> | |
>>> re.findall(caoya_re,'caoya') | |
['caoya'] | |
>>> re.findall(caoya_re,'cao') | |
[] | |
>>> re.findall(caoya_re,'caoya caoya') | |
['caoya', 'caoya'] | |
>>> | |
finditer() : 找到RE匹配的所有子串,并把它们做为一个迭代器返回 | |
>>> caoya_name = re.compile('caoya',re.I) | |
>>> | |
#finditer() 参数把字符串变为迭代器对象返回。 | |
iterator: #A特外忑儿 , 迭代器 | |
>>> caoya_name.finditer('caoya beby') | |
<callable-iterator object at 0x7fb6b4e9bf50> | |
>>> x = caoya_name.finditer('caoya beby') | |
>>> x | |
<callable-iterator object at 0x7fb6b4e9bed0> | |
>>> | |
#next() 参数将迭代器对象转换为 'MatchObject' 马驰对象 | |
>>> x.next() | |
<_sre.SRE_Match object at 0x7fb6b4e8a648> | |
#如果没有匹配到的话,match()和search()将返回None(空)。 | |
如果成功的话,就会返回一个 'MatchObject' 对象 实例。 | |
match object 实例方法: 以下参数在'matchobject'马驰对象 下使用。 | |
group() 古哦普 : 将'MatchObject' 对象 | |
<_sre.SRE_Match object at 0x7fb6b4e8a5e0>返回为字符串'caoya' | |
>>> caoya_name = re.compile('caoya',re.I) | |
>>> | |
>>> caoya_name.match('caoya') | |
<_sre.SRE_Match object at 0x7fb6b4e8a5e0> | |
>>> | |
>>> x = caoya_name.match('caoya') #要定义一个变量等于match 对象。 | |
>>> | |
>>> x | |
<_sre.SRE_Match object at 0x7fb6b4e8a648> | |
>>> x.group() #把match对象的源字符串返回 | |
'caoya' | |
start() :返回匹配开始的位置。 | |
end() :返回匹配结束的位置。 | |
span() :返回一个元祖包含匹配(开始,结束)的位置。 | |
>>> x = caoya_name.match('caoya beby') | |
>>> | |
>>> x.start() | |
0 | |
>>> x.end() | |
5 | |
>>> x.span() | |
(0, 5) | |
>>> | |
print(re.search('www', 'www.runoob.com').span()) # 在起始位置匹配(0,3) | |
print(re.search('com', 'www.runoob.com').span()) # 不在起始位置匹配 (11, 14) | |
#实际程序中,最常见的做法是将'matchobject'马驰对象,保存在一个变量里,然后检查它是否为None. | |
通常我们用'matchobject'马驰对象,是用来判断有这个值或无这个值。 | |
 | |
root@kali:~/xuexi/zhengze# python 2.py | |
No match!! | |
 | |
root@kali:~/xuexi/zhengze# python 2.py | |
有这个值: caoya | |
 | |
root@kali:~/xuexi/zhengze# python 3.py | |
('caoruiqi.group()', 'Ni shi yi tou zhu') | |
('caoruiqi.group(1)', 'Ni') | |
('caoruiqi.group(2)', 'yi') | |
('caoruiqi.group(3)', 'tou') | |
#################################### | |
模块级函数: | |
RE 模块也提供了顶级函数调用如: | |
match() , search() , findall() , sub() , subn() , split() 等 | |
sub() : re模块内的替换函数。 | |
>>> import re | |
>>> | |
>>> caoya = r'h..lo*' #正则开头为h,第四位,五位,lo, *代表o可以无限次重复。 .. 代表两位除了换行符的字符。 | |
>>> | |
#re.sub(正则,"要替换的值",'与规则匹配的值') | |
>>> re.sub(caoya,'nishizhu','hello helloo haalo hhhhh hhhhhh') | |
'nishizhu nishizhu nishizhu hhhhh hhhhhh' | |
>>> re.sub(caoya,'nishizhu','hello helloo haaloooo hhhhh hhhhhh') | |
'nishizhu nishizhu nishizhu hhhhh hhhhhh' | |
subn() : 记录sub() 替换了几次。 | |
>>> re.subn(caoya,'nishizhu','hello helloo haaloooo hhhhh hhhhhh') | |
('nishizhu nishizhu nishizhu hhhhh hhhhhh', 3) | |
>>> re.subn(caoya,'nishizhu','hello helloo hhhhh hhhhhh') | |
('nishizhu nishizhu hhhhh hhhhhh', 2) | |
>>> | |
变量名.split() :切割函数,不运用RE模块,切割只能只对一种。 | |
re.split() : RE模块切割函数,可以切割多次,自定义切割 | |
变量名.split() | |
>>> ip = '192.168.0.103' | |
>>> | |
>>> ip.split('.') #以. 开始切割。 | |
['192', '168', '0', '103'] | |
>>> ip = '192-168-0-103' | |
>>> ip.split('-') #以- 开始切割。 | |
['192', '168', '0', '103'] | |
>>> ip = '192+168-0-103' | |
>>> | |
>>> ip.split('+','-') | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: an integer is required | |
>>> | |
re.split() | |
>>> cc = "1234+2345-3456*10/'abc'" | |
>>> | |
#依次以+ - * / 分割 变量cc, 这里因为+和* 是RE模块的元字符,所以我们用到了\ 反斜杠,让他们只作为单纯的符号呈现。 | |
>>> re.split(r'[\+\-\*\/]',cc) | |
['1234', '2345', '3456', '10', "'abc'"] | |
>>> | |
>>> re.split(r'[\+\-\*]',cc) | |
['1234', '2345', '3456', "10/'abc'"] | |
>>> | |
########################################## | |
总结: | |
dir(re) # 显示出RE模块的所有内置属性。 | |
>>> dir(re) | |
['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_locale', '_pattern_type', '_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template'] | |
>>> | |
help(re) #查看re 模块帮助手册。 | |
>>> help(re) | |
............................................................................................................... | |
help(re.match) #查看RE 下的参数使用方法,多用 help() | |
Help on function match in module re: | |
match(pattern, string, flags=0) | |
Try to apply the pattern at the start of the string, returning | |
a match object, or None if no match was found. | |
(END) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment