Python正则表达式笔记

作者: cynthia猫 | 来源:发表于2018-07-22 13:26 被阅读50次

正则表达式呢,属于看一遍忘一遍的东西。那就来作个笔记吧。
在python中,是使用re模块来实现的。

re.match

这个呢,是从字符串的起始位置开始匹配!

  1. 常规匹配
    这个没啥好说的~~
  2. 泛匹配
content = 'hello 1234567 demo'
result = re.match('hello.*demo$', content)
# .* 这里.匹配任意字符,*表示重复多次
  1. 匹配目标
content = 'hello 1234567 demo'
result = re.match('hello\s(\d+)\sdemo$', content)
print(result.group(1))

> 1234567
# 使用()把目标括起来
  1. 贪婪匹配
content = 'hello 1234567 demo'
result = re.match('h.*(\d+).*o$', content)
print(result.group(1))

>7
# 7前面的数字都被.*匹配走了,所以(\d+)只匹配到一个7
  1. 非贪婪匹配
content = 'hello 1234567 demo'
result = re.match('h.*?(\d+).*o$', content)
print(result.group(1))

>1234567
# 加了问号,就是非贪婪匹配。尽量使用这种方法,避免想要匹配字符没有被匹配到
  1. 匹配模式
content = 'hello 1234567 
demo'
result = re.match('h.*?(\d+).*o$', content, re.S)
print(result.group(1))

>1234567
# 这里如果不使用匹配模式,用.是无法匹配到换行符的,必须要使用了匹配模式才可以匹配到
  1. 转义
content = 'hello price is $5.00'
result = re.match('h.*?(\$5\.00)$', content, re.S)
print(result.group(1))

>$5.00
# 要转义的字符,前面用\

re.search

扫描整个字符串,并返回第一个成功的匹配。
为匹配方便,能用search就不用match

re.findall

content = " \n<li data-view = '1' singer='art1'>song1</li>\n<li data-view = '2' singer='art2'>song2</li>\n<li data-view = '13 singer='art3'>song3</li>\n<li data-view = '7' singer='art4'>song4</li>\n"
results = re.findall('<li.*?singer=(.*?)>(.*?)</li>', content, re.S)
print(results)

>[("'art1'", 'song1'), ("'art2'", 'song2'), ("'art3'", 'song3'), ("'art4'", 'song4')]

re.sub

替换字符串中每一个匹配的子串后,返回替换后的字符串

content = 'strings hello 1234567 this is a demo'
content = re.sub('\d+', '', content)
print(content)
>strings hello  this is a demo
content = 'strings hello 1234567 this is a demo'
content = re.sub('\d+', 'replace', content)
print(content)
>strings hello replace this is a demo
content = 'strings hello 1234567 this is a demo'
content = re.sub('(\d+)', r'\1 987', content)
print(content)
>strings hello 1234567 987 this is a demo
# r是一个转义字符,\1表示把第一个括号里的内容拿出来
content = '<a href = "1.jpg"><li data="1">dakfjalflal</li>abode</a>'
content = re.sub('<a.*?>|</a>', '', content)
print(content)

re.compile

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

content = 'hello 1234567 \ndemo'
pattern = re.compile('hello.*demo', re.S)
result = re.match(pattern, content)
print(result)

相关文章

  • Python ☞ day 11

    Python学习笔记之 正则表达式 re模块概述:Python自1.5以后增加了re的模块,提供了正则表达式模式...

  • 正则表达式

    python学习笔记-正则表达式 标签 : python 特殊符号和字符 re模块:核心函数和方法

  • 正则表达式

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

  • 正则表达式

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

  • 爬虫基础系列正则表达式——爬取简书个人文章标题

    正则表达式简单介绍 正则表达式我在VBA中有详细的阐述《VBA正则笔记》,在python语言中,它的方法与VBA大...

  • Python正则表达式指南

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

  • Python3.5笔记——第11章 正则表达式

    Python3.5笔记 第11章 正则表达式 认识正则表达式 正则表达式是一个特殊字符序列,能够帮助用户检查一个字...

  • 正则表达式

    笔记 正则表达式:用来做字符串查找、匹配、切割用的一种工具。 python对正则表达式的支持:提供了re模块(py...

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

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

  • python正则表达式

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

网友评论

    本文标题:Python正则表达式笔记

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