美文网首页python热爱者Python新世界
正则表达式,python全篇!

正则表达式,python全篇!

作者: 48e0a32026ae | 来源:发表于2018-10-26 15:28 被阅读22次

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

学习Python中有不明白推荐加入交流群

                号:516107834

                群里有志同道合的小伙伴,互帮互助,

                群里有不错的学习教程!

re.match尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none

参数介绍:

pattern:正则表达式

string:匹配的目标字符串

flags:匹配模式

正则表达式的匹配模式:

最常规的匹配

import re

content ='hello 123456 World_This is a Regex Demo'

print(len(content))

result = re.match('^hellosd{6}sw{10}.*Demo$$',content)

print(result)

print(result.group()) #返回匹配结果

print(result.span()) #返回匹配结果的范围

结果运行如下:

<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>

hello 123456 World_This is a Regex Demo

(0, 39)

泛匹配

使用(.*)匹配更多内容

import re

content ='hello 123456 World_This is a Regex Demo'

result = re.match('^hello.*Demo$',content)

print(result)

print(result.group())

结果运行如下:

<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>

hello 123456 World_This is a Regex Demo

匹配目标

在正则表达式中使用()将要获取的内容括起来

使用group(1)获取第一处,group(2)获取第二处,如此可以提取我们想要获取的内容

import re

content ='hello 123456 World_This is a Regex Demo'

result = re.match('^hellos(d{6})s.*Demo$',content)

print(result)

print(result.group(1))#获取匹配目标

结果运行如下:

<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>

123456

贪婪匹配

import re

content ='hello 123456 World_This is a Regex Demo'

result = re.match('^he.*(d+).*Demo$',content)

print(result)

print(result.group(1))

注意:.*会尽可能的多匹配字符

非贪婪匹配

import re

content ='hello 123456 World_This is a Regex Demo'

result = re.match('^he.*?(d+).*Demo$',content)

print(result)

print(result.group(1))

注意:.*?会尽可能匹配少的字符

使用匹配模式

在解析HTML代码时会有换行,这时我们就要使用re.S

import re

content ='hello 123456 World_This '

'is a Regex Demo'

result = re.match('^he.*?(d+).*?Demo$',content,re.S)

print(result)

print(result.group(1))

运行结果如下:

<_sre.SRE_Match object; span=(0, 39), match='hello 123456 World_This is a Regex Demo'>

123456

转义

在解析过程中遇到特殊字符,就需要做转义,比如下面的$符号。

import re

content = 'price is $5.00'

result = re.match('^price.*$5.00',content)

print(result.group())

总结:尽量使用泛匹配,使用括号得到匹配目标,尽量使用非贪婪模式,有换行就用re.S

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

re.search扫描整个字符串并返回第一个成功的匹配。

比如我想要提取字符串中的123456,使用match方法无法提取,只能使用search方法。

import re

content ='hello 123456 World_This is a Regex Demo'

result = re.match('d{6}',content)

print(result)

import re

content ='hello 123456 World_This is a Regex Demo'

result = re.search('d{6}',content)

print(result)

print(result.group())

运行结果如下:

<_sre.SRE_Match object; span=(6, 12), match='123456'>

123456

匹配演练

可以匹配代码里结构相同的部分,这样可以返回你需要的内容

import re

content = '2009年中信出版社出版图书'

result = re.search('(.*?)',content)

print(result.group(1))

1

2009年中信出版社出版图书

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

搜索字符串,以列表形式返回全部能匹配的字串

import re

html ='''

  • 网络歌曲

  • 2009年中信出版社出版图书

    '''

    result = re.findall('(.*?)',html,re.S)

    count = 0

    for list in result:

    print(result[count])

    count+=1

    网络歌曲

    2009年中信出版社出版图书

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

    re.sub共有五个参数

    三个必选参数 pattern,repl,string

    两个可选参数count,flags

    替换字符串中每一个匹配的字符串后替换后的字符串

    import re

    content = 'hello 123456 World_This is a Regex Demo'

    content = re.sub('d+','',content)

    print(content)

    运行结果如下:

    hello World_This is a Regex Demo

    import re

    content = 'hello 123456 World_This is a Regex Demo'

    content = re.sub('d+','what',content)

    print(content)

    运行结果如下:

    hello what World_This is a Regex Demo

    import re

    content = 'hello 123456 World_This is a Regex Demo'

    content = re.sub('(d+)',r'� 789',content)

    print(content)

    运行结果如下:

    hello 123456 789 World_This is a Regex Demo

    注意:这里�代表前面匹配的123456

    Robospider

    演练

    在这里我们替换li标签

    import re

    html ='''

  • 网络歌曲

  • 2009年中信出版社出版图书

    '''

    html = re.sub('

  • |
  • ','',html)

    print(html)

    运行结果如下,里面就没有li标签

    网络歌曲

    2009年中信出版社出版图书

    compile(pattern [, flags])

    该函数根据包含的正则表达式的字符串创建模式对象。可以实现更有效率的匹配

    将正则表达式编译成正则表达式对象,以便于复用该匹配模式

    import re

    content = 'hello 123456 '

    'World_This is a Regex Demo'

    pattern = re.compile('hello.*?Demo',re.S)

    result = re.match(pattern,content)

    print(result.group())

    运行结果如下:

    hello 123456 World_This is a Regex Demo

    综合使用

    import re

    html = '''

    解除好友2:暗网

    7.9

    镰仓物语

    6.9

    特工

    8.3

    幸福的拉扎罗

    8.6

    大师兄

    5.2

    风语咒

    6.9

    精灵旅社3:疯狂假期

    6.8

    狄仁杰之四大天王

    6.2

    摩天营救

    6.4

    复仇者联盟3:无限战争

    8.1

    '''

    count = 0

    for list in result:

    print(result[count])

    count+=1

    运行结果如下:

    ('解除好友2:暗网', '7.9')

    ('镰仓物语', '6.9')

    ('特工', '8.3')

    ('幸福的拉扎罗', '8.6')

    ('大师兄', '5.2')

    ('风语咒', '6.9')

    ('精灵旅社3:疯狂假期', '6.8')

    ('狄仁杰之四大天王', '6.2')

    ('摩天营救', '6.4')

    ('复仇者联盟3:无限战争', '8.1')

    相关文章

    • 正则表达式,python全篇!

      re.match(pattern,string,flags=0) 学习Python中有不明白推荐加入交流群 ...

    • 正则表达式

      Python正则表达式初识(一) Python正则表达式初识(二) Python正则表达式初识(三) Python...

    • 正则表达式

      Python:正则表达式Python:正则表达式

    • Python正则表达式指南

      Python正则表达式指南 本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达...

    • Python爬虫(十)_正则表达式

      本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

    • python正则表达式

      本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

    • [转]python正则表达式(一) 函数使用

      原文:python | 史上最全的正则表达式 更全的正则表达式处理函数:在python中使用正则表达式(一) 0....

    • Python正则表达式

      python正则表达式

    • Python正则表达式用法详解

      搞懂Python 正则表达式用法 Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一...

    • Python正则表达式指南

      本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例...

    网友评论

      本文标题:正则表达式,python全篇!

      本文链接:https://www.haomeiwen.com/subject/fhrctqtx.html