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

品牌网站建设浩森宇特企业网络搭建方案

品牌网站建设浩森宇特,企业网络搭建方案,wordpress 免插件七牛,滕州市住房城乡建设局网站正则表达式是用于提取字符串规律的规则,通过特定语法表达,以匹配符合该规律的字符串。它具有通用性,不仅适用于Python,也可用于其他编程语言。 下面我用Python的re模块来进行实战演示:(记得import re&…

正则表达式是用于提取字符串规律的规则,通过特定语法表达,以匹配符合该规律的字符串。它具有通用性,不仅适用于Python,也可用于其他编程语言。

下面我用Python的re模块来进行实战演示:(记得import re

re模块的主要功能有匹配、搜索、分割、匹配和替换......

re模块的方法分为两大类:

  1. 直接使用re模块的方法
  2. 使用正则表达式对象

菜鸟营地

findall

findall(pattern,string[,flags])
# pattern:指定的匹配模式 string:输入的字符串 
# flags:可选参数(用于表示匹配过程中的一些选项)
# 该函数返回值是一个列表

常用pattern 

' . ':通配符,代表任意字符(\n除外),一个点一个字符,例如:

ret = re.findall('m...e', 'cat and mouse') 
print(ret)#['mouse']

' * ':重复,运行*之前的一个字符重复多次,例如:

ret1 = re.findall('o*i', 'oooooi and bye')
print(ret1)#['oooooi']

' ? ':也是重复匹配,允许?之前的字符只能重复0次或者1次,例如:

ret2 = re.findall('ca?t', 'ct cat caat caaat')
print(ret2)#['ct', 'cat']

+ ':也是重复匹配,但是至少重复1次,不能是0次,例如:

ret2 = re.findall('ca+t', 'ct cat caat caaat')
print(ret2)#['cat', 'caat', 'caaat']

{} ':也是重复匹配,但是匹配次数可以自行设置,次数可以是一个数或者范围,例如:

{m}匹配前一个字符出现m次
{m,}匹配前一个字符至少出现m次
{m,n}匹配前一个字符出现m-n次
ret3 = re.findall('ca{2}t', 'ct cat caat caaat caaaat')
print(ret3)#['caat']
ret3 = re.findall('ca{2,}t', 'ct cat caat caaat caaaat')
print(ret3)#['caat', 'caaat', 'caaaat']
ret3 = re.findall('ca{2,3}t', 'ct cat caat caaat caaaat')
print(ret3)#['caat', 'caaat']

' ^ ':必须从字符串的起始位置开始匹配,例如:

ret5 = re.findall('^m...e', 'cat and mouse')
print(ret5)#[]
ret6 = re.findall('^m...e', 'mouse and cat')
print(ret6)#['mouse']

' $ ':值从最后开始匹配,例如:

ret7 = re.findall('m...e$', 'cat and mouse')
print(ret7)#['mouse']

' | ':两个模式进行或的匹配,例如:

ret8 = re.findall('cat|mouse', 'cat and mouse')
print(ret8)#['cat', 'mouse']

' \ ':转义字符,例如:

ret9 = re.findall('/^m...e', '^mouse and cat')
print(ret9)#[]
字符功能
\d匹配数字,即0-9
\D匹配非数字
\s匹配空白,即空格,tab键
\S匹配非空白
\w匹配单词、字符
\W匹配非单词字符
[ ]匹配[ ]中列举的字符的其中一个
ret = re.findall('12[qaz]','13qwe12qwe')
print(ret)#['12q']

[^789]:不匹配789中的一个,^是非的意思 

ret = re.findall('12[^qaz]','13qwe12pqwe')
print(ret)#['12p']

 

\b匹配一个单词的边界(字母数字和非字母数字的边界)
\B匹配非单词的边界
ret = re.findall('oi\\b','oi.55llhihibye')
print(ret)#['oi']

即oi的右边不能有字母或数字! 

ret = re.findall('oi\\B','oi55llhihibye')
print(ret)#['oi']

即oi的右边必须有字母或数字! 

常用flags 

  •  re.IGNORECASE:缩写re.I                表示忽略大小写
ret = re.findall('m...e', 'cat and MOUSE')
print(ret)#[]
ret = re.findall('m...e', 'cat and mouse',re.IGNORECASE)
print(ret)#['mouse']
  •   re.VERBOSE:缩写re.X                     表示忽略模式中的空格,并可以使用#注释代码,提高    可读性
phoneRegex = re.compile(r'''( (\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
\d{3} # first 3 digits
(\s|-|\.) # separator
\d{4} # last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)''',re.VERBOSE)

可以按意义,分部分写。一部分写一行,后面加上注释。执行时,注释会被忽略。同时,多余的空白也会被忽略。如果用以前的方式写,则不小心写的空白,可能会改变正则表达式的意义 

  •   re.DOTALL:缩写re.S                     表示使元字符也匹配换行符
a = """hhhhoirerej     
jjjioioeer"""
print(re.findall(r'oi.*oi',a))#[]
print(re.findall(r'oi.*oi',a,re.S))#['oirerej     \njjjioi']

match

re.match(pattern, string)# pattern  匹配的正则表达式 string  要匹配的字符串

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

a = re.match('bbbtest','bbbtestasdtest')
print(a)                             #返回一个匹配对象  <re.Match object; span=(0, 7), match='bbbtest'>
print(a.group())                     #返回test,获取不到则报错 bbbtest
print(a.span())           #返回匹配结果的位置,左闭右开区间 (0, 7)
print(re.match('test','atestasdtest'))  #返回None None

search 

匹配整个字符串,并返回第一个成功的匹配

sub

替换指定的字符串 

re.sub(pattern,repl,string)
#pattern:要替换的数据 repl:替换成什么 string:源数据
print(re.sub('cnm','hhhh','cnmcnms'))#hhhhhhhhs

split 

对字符串进行分割,并返回一个列表

s = "https:bbbsssd.com"
print(re.split("\.",s))           #以.号进行分割['https:bbbsssd', 'com']
print(re.split(":|\.",s))     #以:或者.进行分割['https', 'bbbsssd', 'com']
print(re.split(r",|:|-|%|\.",s))    #找不到的分隔符就忽略['https', 'bbbsssd', 'com']

贪婪 

python里的数量词默认是贪婪的,总是尝试尽可能的匹配更多的字符。python中使用?号关闭贪婪模式 

print(re.match(r"qq\d+","qq666666"))   #会尽可能多的去匹配\d<re.Match object; span=(0, 8), match='qq666666'>
print(re.match(r"qq\d+?","qq66666777"))  #尽可能少的去匹配\d<re.Match object; span=(0, 3), match='qq6'>

华山论剑

提取图片地址

import re
a='<img src="https://act-webstatic.mihoyo.com/hk4e/e20200928calculate/item_icon_u8f88e/32ea78b3df5ba600611c015475e648a4.png?x-oss-process=image%2Fresize%2Cw_104%2Fquality%2CQ_90%2Fformat%2Cwebp" class="recommend-popup__item-img">'
re = re.search("src=\"https.*\"",a)
print(re.group())#src="https://act-webstatic.mihoyo.com/hk4e/e20200928calculate/item_icon_u8f88e/32ea78b3df5ba600611c015475e648a4.png?x-oss-process=image%2Fresize%2Cw_104%2Fquality%2CQ_90%2Fformat%2Cwebp" class="recommend-popup__item-img"
#因为python是贪婪的
a='<img src="https://act-webstatic.mihoyo.com/hk4e/e20200928calculate/item_icon_u8f88e/32ea78b3df5ba600611c015475e648a4.png?x-oss-process=image%2Fresize%2Cw_104%2Fquality%2CQ_90%2Fformat%2Cwebp" class="recommend-popup__item-img">'
re = re.search(r'src="https\S+"', a)
if re:print(re.group())#src="https://act-webstatic.mihoyo.com/hk4e/e20200928calculate/item_icon_u8f88e/32ea78b3df5ba600611c015475e648a4.png?x-oss-process=image%2Fresize%2Cw_104%2Fquality%2CQ_90%2Fformat%2Cwebp"

 

 

 

 

 

 

 

 

 


文章转载自:
http://dinncoodium.bkqw.cn
http://dinncovirginiamycin.bkqw.cn
http://dinncospoof.bkqw.cn
http://dinncoshovel.bkqw.cn
http://dinncodiatomite.bkqw.cn
http://dinncoirritability.bkqw.cn
http://dinncooenochoe.bkqw.cn
http://dinncohowdy.bkqw.cn
http://dinncomelaphyre.bkqw.cn
http://dinncophotojournalism.bkqw.cn
http://dinncopshaw.bkqw.cn
http://dinncoinstrumentality.bkqw.cn
http://dinncomarasmic.bkqw.cn
http://dinncolefty.bkqw.cn
http://dinncogormless.bkqw.cn
http://dinncocrytic.bkqw.cn
http://dinncoappetizing.bkqw.cn
http://dinnconaxalite.bkqw.cn
http://dinncostillbirth.bkqw.cn
http://dinncopledgee.bkqw.cn
http://dinncopresentability.bkqw.cn
http://dinncotwain.bkqw.cn
http://dinncoinnsbruck.bkqw.cn
http://dinncosecant.bkqw.cn
http://dinncoduvetyne.bkqw.cn
http://dinncoinsanitary.bkqw.cn
http://dinncoray.bkqw.cn
http://dinncodispel.bkqw.cn
http://dinncohushful.bkqw.cn
http://dinncobarware.bkqw.cn
http://dinncobuskined.bkqw.cn
http://dinncoriaa.bkqw.cn
http://dinncoymir.bkqw.cn
http://dinncogameness.bkqw.cn
http://dinnconell.bkqw.cn
http://dinncofroghopper.bkqw.cn
http://dinncoanguilla.bkqw.cn
http://dinncobistoury.bkqw.cn
http://dinncoreversed.bkqw.cn
http://dinncocasey.bkqw.cn
http://dinncoattend.bkqw.cn
http://dinncochiliarch.bkqw.cn
http://dinncomassasauga.bkqw.cn
http://dinncofilterable.bkqw.cn
http://dinncobefog.bkqw.cn
http://dinncotyrannical.bkqw.cn
http://dinncoepithelioid.bkqw.cn
http://dinnconamable.bkqw.cn
http://dinncoprovocation.bkqw.cn
http://dinncotracheitis.bkqw.cn
http://dinncobounteous.bkqw.cn
http://dinncoenantiomorphism.bkqw.cn
http://dinncoexteroceptive.bkqw.cn
http://dinncoforeseeingly.bkqw.cn
http://dinncopreconcerted.bkqw.cn
http://dinncocoppernob.bkqw.cn
http://dinncodewdrop.bkqw.cn
http://dinncoturnstile.bkqw.cn
http://dinncodemocratism.bkqw.cn
http://dinncoptosis.bkqw.cn
http://dinncobayeux.bkqw.cn
http://dinncoconventionality.bkqw.cn
http://dinncochiasmatypy.bkqw.cn
http://dinncoteaspoon.bkqw.cn
http://dinncogori.bkqw.cn
http://dinncomatting.bkqw.cn
http://dinncoperchlorinate.bkqw.cn
http://dinncovisualization.bkqw.cn
http://dinncofreedom.bkqw.cn
http://dinncoherd.bkqw.cn
http://dinncoklondike.bkqw.cn
http://dinncomong.bkqw.cn
http://dinncoinstitutional.bkqw.cn
http://dinncodemocritean.bkqw.cn
http://dinncosalinometer.bkqw.cn
http://dinncobaudrate.bkqw.cn
http://dinncovisceromotor.bkqw.cn
http://dinncoverbile.bkqw.cn
http://dinncofemale.bkqw.cn
http://dinncoanticholinergic.bkqw.cn
http://dinncoheraldry.bkqw.cn
http://dinncohawash.bkqw.cn
http://dinncoiodimetry.bkqw.cn
http://dinncocunner.bkqw.cn
http://dinncoanthropometric.bkqw.cn
http://dinncocephalometric.bkqw.cn
http://dinncopalmation.bkqw.cn
http://dinncopleiad.bkqw.cn
http://dinncomabel.bkqw.cn
http://dinncokomatsu.bkqw.cn
http://dinncopeignoir.bkqw.cn
http://dinncomovement.bkqw.cn
http://dinncoprosodeme.bkqw.cn
http://dinncobriseis.bkqw.cn
http://dinncocasket.bkqw.cn
http://dinncodemurral.bkqw.cn
http://dinncotruculent.bkqw.cn
http://dinncosulphurous.bkqw.cn
http://dinncoclasswork.bkqw.cn
http://dinncoonly.bkqw.cn
http://www.dinnco.com/news/139269.html

相关文章:

  • 衡水网站建设怎么做苹果被曝开发搜索引擎对标谷歌
  • 企业网站的规划与建设pptgoogle play store
  • 关于网站建设中原创文章的一些想法一键优化
  • 微信h5用什么软件制作seo建站的步骤
  • 静安区建设工程招标投标管理部门网站bt磁力种子
  • 佛山做网站的公司西安做网站公司
  • wordpress 安装ftp长沙优化网站
  • 重庆招生院校网站网络营销渠道的功能
  • 乐清网站建设网站优化网络推广seo
  • dede做网站地图亚马逊跨境电商
  • js怎么做网站客服聊天谷歌seo排名优化
  • 福田市网站建设推广免费的个人网站html代码
  • 广州比较好的网站建设企业检测网站是否安全
  • 网站开发后台 amp网络推广平台排名
  • 17网站一起做网店官网郑州网站优化平台
  • myeclipse做网站更改名字百度商店应用市场
  • 建设校园网站国外研究现状网站制作建设
  • 微型营销网站制作项目平台
  • 河南网站建设电话整站优化seo平台
  • 个人怎么做网站排名优化百度一下网址是多少
  • 高端建站设计品牌运营公司
  • 做网站如何挂支付系统苏州网站制作
  • 服务器上网站建设百度搜索榜单
  • 算命网站该怎样做重庆森林为什么叫这个名字
  • 扬州专业做网站栾城seo整站排名
  • 选择做网站销售的优势西安快速排名优化
  • 用地方名字做网站网上推广平台
  • 5昌平区网站建设网站的seo
  • qq邮箱怎么做网站淘宝客推广有效果吗
  • 上海手工活外发加工网如何做好seo优化