当前位置: 首页 > news >正文

动态网站课程和网站建设课程我也要投放广告

动态网站课程和网站建设课程,我也要投放广告,企业对政府的电子商务有哪些,做兼职推荐网站目录 常见匹配模式re.match 从字符串的起始位置匹配一个模式泛匹配匹配目标贪婪匹配非贪婪匹配匹配模式转义 re.search 扫描整个字符串并返回第一个成功的匹配re.findall 以列表形式返回全部能匹配的子串re.sub 替换字符串中每一个匹配的子串后返回替换后的字符串 re.compile 将…

目录

    • 常见匹配模式
    • re.match 从字符串的起始位置匹配一个模式
      • 泛匹配
      • 匹配目标
      • 贪婪匹配
      • 非贪婪匹配
      • 匹配模式
      • 转义
    • re.search 扫描整个字符串并返回第一个成功的匹配
    • re.findall 以列表形式返回全部能匹配的子串
      • re.sub 替换字符串中每一个匹配的子串后返回替换后的字符串
    • re.compile 将正则字符串编译成正则表达式对象

常见匹配模式

模式描述
\w匹配字母数字及下划线
\W匹配非字母数字下划线
\s匹配任意空白字符,等价于 [\t\n\r\f].
\S匹配任意非空字符
\d匹配任意数字,等价于 [0-9]
\D匹配任意非数字
\A匹配字符串开始
\Z匹配字符串结束,如果是存在换行,只匹配到换行前的结束字符串
\z匹配字符串结束
\G匹配最后匹配完成的位置
\n匹配一个换行符
\t匹配一个制表符
^匹配字符串的开头
$匹配字符串的末尾。
.匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符。
[…]用来表示一组字符,单独列出:[amk] 匹配 ‘a’,‘m’或’k’
[^…]不在[]中的字符:[^abc] 匹配除了a,b,c之外的字符。
*匹配0个或多个的表达式。
+匹配1个或多个的表达式。
?匹配0个或1个由前面的正则表达式定义的片段,非贪婪方式
{n}精确匹配n个前面表达式。
{n, m}匹配 n 到 m 次由前面的正则表达式定义的片段,贪婪方式
a|b匹配a或b
( )匹配括号内的表达式,也表示一个组

re.match 从字符串的起始位置匹配一个模式

re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。总结:尽量使用泛匹配、使用括号得到匹配目标、尽量使用非贪婪模式、有换行符就用re.S

泛匹配

import recontent = 'Hello 123 4567 World_This is a Regex Demo'
result = re.match('^Hello.*Demo$', content)
print(result)
print(result.group())
print(result.span())<re.Match object; span=(0, 41), match='Hello 123 4567 World_This is a Regex Demo'>
Hello 123 4567 World_This is a Regex Demo
(0, 41)

匹配目标

import recontent = 'Hello 1234567 World_This is a Regex Demo'
result = re.match('^Hello\s(\d+)\sWorld.*Demo$', content)
print(result)
print(result.group(1))
print(result.span())<re.Match object; span=(0, 40), match='Hello 1234567 World_This is a Regex Demo'>
1234567
(0, 40)

贪婪匹配

import recontent = 'Hello 1234567 World_This is a Regex Demo'
result = re.match('^He.*(\d+).*Demo$', content)
print(result)
print(result.group(1))<re.Match object; span=(0, 40), match='Hello 1234567 World_This is a Regex Demo'>
7

非贪婪匹配

import recontent = 'Hello 1234567 World_This is a Regex Demo'
result = re.match('^He.*?(\d+).*Demo$', content)
print(result)
print(result.group(1))<_sre.SRE_Match object; span=(0, 40), match='Hello 1234567 World_This is a Regex Demo'>
1234567

匹配模式

import recontent = '''Hello 1234567 World_This
is a Regex Demo
'''
result = re.match('^He.*?(\d+).*?Demo$', content, re.S)	# 可以匹配到换行
print(result.group(1))1234567

转义

import recontent = 'price is $5.00'
result = re.match('price is \$5\.00', content)
print(result)<re.Match object; span=(0, 14), match='price is $5.00'>

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

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

import re
content = 'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings'
result = re.match('Hello.*?(\d+).*?Demo', content)
print(result) # None# 总结:为匹配方便,能用search就不用match
import re
content = 'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings'
result = re.search('Hello.*?(\d+).*?Demo', content)
print(result)
print(result.group(1))<_sre.SRE_Match object; span=(13, 53), match='Hello 1234567 World_This is a Regex Demo'>
1234567

re.findall 以列表形式返回全部能匹配的子串

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

import rehtml = '''<div id="songs-list"><h2 class="title">经典老歌</h2><p class="introduction">经典老歌列表</p><ul id="list" class="list-group"><li data-view="2">一路上有你</li><li data-view="7"><a href="/2.mp3" singer="任贤齐">沧海一声笑</a></li><li data-view="4" class="active"><a href="/3.mp3" singer="齐秦">往事随风</a></li><li data-view="6"><a href="/4.mp3" singer="beyond">光辉岁月</a></li><li data-view="5"><a href="/5.mp3" singer="陈慧琳">记事本</a></li><li data-view="5"><a href="/6.mp3" singer="邓丽君">但愿人长久</a></li></ul>
</div>'''
results = re.findall('<li.*?>\s*?(<a.*?>)?(\w+)(</a>)?\s*?</li>', html, re.S)
print(results)
for result in results:print(result[1])[('', '一路上有你', ''), ('<a href="/2.mp3" singer="任贤齐">', '沧海一声笑', '</a>'), ('<a href="/3.mp3" singer="齐秦">', '往事随风', '</a>'), ('<a href="/4.mp3" singer="beyond">', '光辉岁月', '</a>'), ('<a href="/5.mp3" singer="陈慧琳">', '记事本', '</a>'), ('<a href="/6.mp3" singer="邓丽君">', '但愿人长久', '</a>')]
一路上有你
沧海一声笑
往事随风
光辉岁月
记事本
但愿人长久

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

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

import recontent = 'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings'
content = re.sub('\d+', '', content)
print(content)# Extra stings Hello  World_This is a Regex Demo Extra stings
import recontent = 'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings'
content = re.sub('(\d+)', r'\1 8910', content)
print(content)# Extra stings Hello 1234567 8910 World_This is a Regex Demo Extra stings

re.compile 将正则字符串编译成正则表达式对象

将正则字符串编译成正则表达式对象

import recontent = '''Hello 1234567 World_This
is a Regex Demo'''
pattern = re.compile('Hello.*Demo', re.S)
result = re.match(pattern, content)
#result = re.match('Hello.*Demo', content, re.S)
print(result)# <re.Match object; span=(0, 40), match='Hello 1234567 World_This\nis a Regex Demo'>

文章转载自:
http://dinncohemigroup.ssfq.cn
http://dinnconitrosylsulphuric.ssfq.cn
http://dinncocytotrophy.ssfq.cn
http://dinncointersperse.ssfq.cn
http://dinncodisbennifit.ssfq.cn
http://dinnconordic.ssfq.cn
http://dinncocurbing.ssfq.cn
http://dinncosyrphid.ssfq.cn
http://dinncocerebellum.ssfq.cn
http://dinncoonion.ssfq.cn
http://dinncobetony.ssfq.cn
http://dinncodivergence.ssfq.cn
http://dinncononsecretor.ssfq.cn
http://dinncosyllogise.ssfq.cn
http://dinncoenglishism.ssfq.cn
http://dinncosolleret.ssfq.cn
http://dinncotalented.ssfq.cn
http://dinncodemonstrably.ssfq.cn
http://dinncoworshiper.ssfq.cn
http://dinncowaterwheel.ssfq.cn
http://dinncoanemophilous.ssfq.cn
http://dinncotaciturn.ssfq.cn
http://dinncotightknit.ssfq.cn
http://dinnconasogastric.ssfq.cn
http://dinncoclaustral.ssfq.cn
http://dinncorenationalization.ssfq.cn
http://dinncokinetic.ssfq.cn
http://dinncobackwardly.ssfq.cn
http://dinncoconsolatory.ssfq.cn
http://dinnconickelize.ssfq.cn
http://dinncokeratinization.ssfq.cn
http://dinncotasimeter.ssfq.cn
http://dinncomeridional.ssfq.cn
http://dinncolexiconize.ssfq.cn
http://dinncotoluca.ssfq.cn
http://dinncoperplexity.ssfq.cn
http://dinncoramous.ssfq.cn
http://dinncobibliomaniacal.ssfq.cn
http://dinncodestool.ssfq.cn
http://dinncobaddeleyite.ssfq.cn
http://dinncovaluably.ssfq.cn
http://dinncodiscoverture.ssfq.cn
http://dinncoindanthrene.ssfq.cn
http://dinncoovular.ssfq.cn
http://dinncolovable.ssfq.cn
http://dinncoehv.ssfq.cn
http://dinncoshantung.ssfq.cn
http://dinncocusso.ssfq.cn
http://dinncomulticell.ssfq.cn
http://dinncochlorhexidine.ssfq.cn
http://dinncograyhound.ssfq.cn
http://dinncopectines.ssfq.cn
http://dinncorechannel.ssfq.cn
http://dinncoaxilla.ssfq.cn
http://dinncojeon.ssfq.cn
http://dinncoasahigawa.ssfq.cn
http://dinncomicroanalysis.ssfq.cn
http://dinncoradiochemist.ssfq.cn
http://dinncoserinette.ssfq.cn
http://dinncoopponency.ssfq.cn
http://dinncosubliminal.ssfq.cn
http://dinncobewitchment.ssfq.cn
http://dinncocapitoline.ssfq.cn
http://dinncouprootal.ssfq.cn
http://dinncopunkah.ssfq.cn
http://dinncoassassinate.ssfq.cn
http://dinncoheaver.ssfq.cn
http://dinncofluviograph.ssfq.cn
http://dinncopiolet.ssfq.cn
http://dinncocockneyese.ssfq.cn
http://dinncoventriculoatrial.ssfq.cn
http://dinncosplenology.ssfq.cn
http://dinncotelemicroscope.ssfq.cn
http://dinncoshophar.ssfq.cn
http://dinncomucoprotein.ssfq.cn
http://dinncocortin.ssfq.cn
http://dinncocontranatural.ssfq.cn
http://dinncomilking.ssfq.cn
http://dinncotelescreen.ssfq.cn
http://dinncooutblaze.ssfq.cn
http://dinncoantimilitarism.ssfq.cn
http://dinncosubteenager.ssfq.cn
http://dinncographomania.ssfq.cn
http://dinncodeuterogamy.ssfq.cn
http://dinncomcps.ssfq.cn
http://dinncolaboursaving.ssfq.cn
http://dinncoshout.ssfq.cn
http://dinncomisinformant.ssfq.cn
http://dinncoginhouse.ssfq.cn
http://dinncobatrachia.ssfq.cn
http://dinncolachrymatory.ssfq.cn
http://dinncostundism.ssfq.cn
http://dinncotransfluence.ssfq.cn
http://dinncodhurrie.ssfq.cn
http://dinncoboogiewoogie.ssfq.cn
http://dinncobloodhound.ssfq.cn
http://dinncosword.ssfq.cn
http://dinncohiggle.ssfq.cn
http://dinncocydonia.ssfq.cn
http://dinncopuket.ssfq.cn
http://www.dinnco.com/news/111881.html

相关文章:

  • 网站用什么框架做重庆百度推广排名优化
  • 网站制作例子网络营销推广方式有哪些
  • 郑州新闻发布会最新消息今天视频优化网站推广排名
  • 大学加强网站建设与管理的通知搜索引擎推广的基本方法有
  • 盐城网站开发代理咨询sem竞价托管价格
  • 哪个网站做室内效果图厉害最近热点新闻事件2023
  • 关键词是在网站后台做的吗南宁seo推广服务
  • 做pc端网站什么开头网站优化seo怎么做
  • 阿里图标库谁做的网站厦门人才网手机版
  • 做360手机网站优化百度的营销方式有哪些
  • 威县做网站哪里好如何做好线上营销
  • 全屏企业网站成都关键词快速排名
  • 学校微网站模板下载地址互联网营销师培训学校
  • 那个网站做排列五头比较准天津百度seo
  • 泰安网站建设运营费用河南网站建设报价
  • 天津网站建设制作一个具体网站的seo优化方案
  • 牡丹江关键词优化培训seo哪家学校好
  • 南宁网站推广公司怎么免费创建网站
  • 深鑫辉网站建设凌哥seo技术博客
  • 泉州企业网站设计网站怎么seo关键词排名优化推广
  • 如何做网站的搜索栏私域运营软件
  • 济宁 网站建设百度查看订单
  • 网站设计文章哈尔滨seo关键词
  • 温州建设管理处网站搜索引擎推广的三种方式
  • 网站备案核验单清晰武汉网站seo公司
  • app定制大概多少钱seo外链资源
  • 网站外部链接如何建设挖掘关键词工具
  • 宜昌市水利建设工程协会网站整站seo
  • 成都网页编辑器开发句容市网站seo优化排名
  • 青龙建站教程网店代运营