app开发定制公司Python 正则表达式详解(建议收藏!)

目录


app开发定制公司是对字符串提取的一套规则,app开发定制公司我们把这个规则用正则app开发定制公司里面的特定语法表达出来,app开发定制公司去匹配满足这个规则的字符串。app开发定制公司正则表达式具有通用型,不仅pythonapp开发定制公司里面可以用,app开发定制公司其他的语言也一样适用。

python中re模块app开发定制公司提供了正则表达式的功能,app开发定制公司常用的有四个方法(match、search、findall)app开发定制公司都可以用于app开发定制公司匹配字符串

match

匹配字符串

re.match()app开发定制公司必须从字符串开头匹配!match方法尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。主要参数如下:

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

例子

  1. import re
  2. a = re.match('test','testasdtest')
  3. print(a) #返回一个匹配对象
  4. print(a.group()) #返回test,获取不到则报错
  5. print(a.span()) #返回匹配结果的位置,左闭右开区间
  6. print(re.match('test','atestasdtest')) #返回None

从例子中我们可以看出,re.match()方法返回一个匹配的对象,而不是匹配的内容。如果需要返回内容则需要调用group()。通过调用span()可以获得匹配结果的位置。而如果从起始位置开始没有匹配成功,即便其他部分包含需要匹配的内容,re.match()也会返回None。

单字符匹配

以下字符,都匹配单个字符数据。且开头(从字符串0位置开始)没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none

. 匹配任意一个字符

 使用几个点号就代表几个字符

  1. import re
  2. a = re.match('..','testasdtest')
  3. print(a.group()) #输出te
  4. b = re.match('ab.','testasdtest')
  5. print(b) #返回none,因为表达式是以固定的ab开头然后跟上通配符. 所以必须要先匹配上ab才会往后进行匹配

\d 匹配数字

 一个\d代表一个数字。开头没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none

  1. import re
  2. a = re.match('\d\d','23es12testasdtest')
  3. print(a)
  4. b = re.match('\d\d\d','23es12testasdtest')
  5. print(b) #要求匹配三个数字,匹配不到返回none
  6. c = re.match('\d','es12testasdtest')
  7. print(c) #起始位置没有匹配成功,一样返回none

\D 匹配非数字

开头没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none

  1. import re
  2. a = re.match('\D','23es12testasdtest')
  3. print(a) #开头为数字所以返回none
  4. b = re.match('\D\D','*es12testasdtest')
  5. print(b) #返回*e

\s 匹配特殊字符,如空白,空格,tab等

  1. import re
  2. print(re.match('\s',' 23es 12testasdtest')) #匹配空格
  3. print(re.match('\s',' 23es 12testasdtest')) #匹配tab
  4. print(re.match('\s','\r23es 12testasdtest')) #匹配\r换行
  5. print(re.match('\s','23es 12testasdtest')) #返回none

\S 匹配非空白

  1. import re
  2. print(re.match('\S',' 23es 12testasdtest')) #返回none
  3. print(re.match('\S','\r23es 12testasdtest')) #none
  4. print(re.match('\S','23es 12testasdtest'))

\w 匹配单词、字符,如大小写字母,数字,_ 下划线

  1. import re
  2. print(re.match('\w','23es 12testasdtest')) #返回none
  3. print(re.match('\w\w\w','aA_3es 12testasdtest')) #返回none
  4. print(re.match('\w\w\w',' testasdtest')) #返回none

\W 匹配非单词字符

  1. import re
  2. print(re.match('\W','23es 12testasdtest')) #返回none
  3. print(re.match('\W',' 23es 12testasdtest')) #匹配空格

[ ] 匹配[ ]中列举的字符

只允许出现[ ]中列举的字符

  1. import re
  2. print(re.match('12[234]','232s12testasdtest')) #因为开头的12没匹配上,所以直接返回none
  3. print(re.match('12[234]','1232s12testasdtest')) #返回123

[^2345] 不匹配2345中的任意一个

  1. import re
  2. print(re.match('12[^234]','232s12testasdtest')) #因为开头的12没匹配上,所以直接返回none
  3. print(re.match('12[^234]','1232s12testasdtest')) #返回none
  4. print(re.match('12[^234]','1252s12testasdtest')) #返回125

[a-z3-5] 匹配a-z或者3-5中的字符

  1. import re
  2. print(re.match('12[1-3a-c]','1232b12testasdtest')) #123
  3. print(re.match('12[1-3a-c]','12b2b12testasdtest')) #12b
  4. print(re.match('12[1-3a-c]','12s2b12testasdtest')) #返回none

表示数量

 像上面写的那些都是匹配单个字符,如果我们要匹配多个字符的话,只能重复写匹配符。这样显然是不人性化的,所以我们还需要学习表达数量的字符

 * 出现0次或无数次

  1. import re
  2. a = re.match('..','testasdtest')
  3. print(a.group()) #输出te
  4. a = re.match('.*','testasdtest')
  5. print(a.group()) #全部输出

  1. import re
  2. print(re.match('a*','aatestasdtest')) #匹配跟随在字母a后面的所有a字符
  3. print(re.match('\d*','23aatestasdtest')) #匹配前面为数字的字符
  4. print(re.match('a\d*','ad23aatestasdtest')) #输出a, 因为*也可以代表0次

+ 至少出现一次

  1. import re
  2. print(re.match('a+','aaatestasdtest')) #匹配前面为字母a的字符,且a至少有1一个
  3. print(re.match('a+','atestasdtest')) #a
  4. print(re.match('a+','caaatestasdtest')) #none

? 1次或则0次

  1. import re
  2. print(re.match('a?','abatestasdtest')) #匹配a出现0次或者1次数
  3. print(re.match('a?','batestasdtest')) #输出空,因为a可以为0次
  4. print(re.match('a?','aaatestasdtest')) #a出现0次或者1次,输出1个a

{m}指定出现m次

  1. import re
  2. print(re.match('to{3}','toooooabatestasdtest')) #匹配t以及跟随在后面的三个ooo
  3. print(re.match('to{3}','tooabatestasdtest')) #只有两个0,返回none

{m,} 至少出现m次

  1. import re
  2. print(re.match('to{3,}','toooooabatestasdtest')) #匹配t以及跟随在后面的三个ooo至少出现3次
  3. print(re.match('to{3,}','tooabatestasdtest')) #只有两个0,返回none

{m,n} 指定从m-n次的范围

  1. import re
  2. print(re.match('to{3,4}','toooabatestasdtest')) #刚好有三个ooo,成功匹配
  3. print(re.match('to{3,4}','tooabatestasdtest')) #只有两个o,返回none
  4. print(re.match('to{3,4}','toooooabatestasdtest')) #提取最多四个o

匹配边界

$ 匹配结尾字符

定义整个字符串必须以指定字符串结尾

  1. import re
  2. print(re.match('.*d$','2testaabcd')) #字符串必须以d结尾
  3. print(re.match('.*c','2testaabcd')) #字符串不是以c结尾,返回none

^ 匹配开头字符

定义整个字符串必须以指定字符开头

  1. import re
  2. print(re.match('^2','2stoooabatestas')) #规定必须以2开头,否则none
  3. print(re.match('^2s','2stoooabatestas')) #必须以2s开头

\b 匹配一个单词的边界

\b:表示字母数字与非字母数字的边界,非字母数字与字母数字的边界。即下面ve的右边不能有字母和数字

  1. import re
  2. print(re.match(r'.*ve\b','ve.2testaabcd')) #因为在python中\代表转义,所以前面加上r消除转义
  3. print(re.match(r'.*ve\b','ve2testaabcd'))

\B 匹配非单词边界

  1. import re
  2. print(re.match(r'.*ve\B','2testaavebcdve')) #ve的右边需要有字母或者数字
  3. print(re.match(r'.*ve\B','2testaave3bcdve'))

匹配分组

 

| 匹配左右任意一个表达式

只要|两边任意一个表达式符合要求就行

  1. import re
  2. print(re.match(r'\d[1-9]|\D[a-z]','2233')) #匹配|两边任意一个表达式
  3. print(re.match(r'\d[1-9]|\D[a-z]','as'))

(ab) 将括号中字符作为一个分组

()中的内容会作为一个元组字符装在元组中

  1. import re
  2. a = re.match(r'<h1>(.*)<h1>','<h1>你好啊<h1>')
  3. print(a.group()) #输出匹配的字符
  4. print(a.groups()) #会将()中的内容会作为一个元组字符装在元组中
  5. print('`````````````')
  6. b = re.match(r'<h1>(.*)(<h1>)','<h1>你好啊<h1>')
  7. print(b.groups()) #有两括号就分为两个元组元素
  8. print(b.group(0)) #group中默认是0
  9. print(b.group(1)) #你好啊
  10. print(b.group(2)) #h1

search

和match差不多用法,从字符串中进行搜索

  1. import re
  2. print(re.match(r'\d\d','123test123test'))
  3. print(re.search(r'\d\d','123test123test'))

findall

从字面意思上就可以看到,findall是寻找所有能匹配到的字符,并以列表的方式返回

  1. import re
  2. print(re.search(r'test','123test123test'))
  3. print(re.findall(r'test','123test123test')) #以列表的方式返回

re.s

findall中另外一个属性re.S

在字符串a中,包含换行符,在这种情况下

  • 如果不使用re.S参数,则只在每一行内进行匹配,如果一行没有,就换下一行重新开始。
  • 而使用re.S参数以后,正则表达式会将这个字符串作为一个整体,在整体中进行匹配。

 如下要寻找test.*123的数据,因为test和123在不同的行,如果没加re.s的话,他会在每一个进行匹配查找而不是将字符串作为一个整体进行查找

  1. import re
  2. a = """aaatestaa
  3. aaaa123"""
  4. print(re.findall(r'test.*123',a))
  5. print(re.findall(r'test.*123',a,re.S))

sub

查找字符串中所有相匹配的数据进行替换

sub(要替换的数据,替换成什么,要替换的数据所在的数据)

  1. import re
  2. print(re.sub('php','python','php是世界上最好的语言——php'))
  3. #输出 "python是世界上最好的语言——python"

split

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

  1. import re
  2. s = "itcase,java:php-php3;html"
  3. print(re.split(r",",s)) #以,号进行分割
  4. print(re.split(r",|:|-|;",s)) #以,或者:或者-或者;进行分割
  5. print(re.split(r",|:|-|%",s)) #找不到的分隔符就忽略

贪婪与非贪婪

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

  1. import re
  2. print(re.match(r"aa\d+","aa2323")) #会尽可能多的去匹配\d
  3. print(re.match(r"aa\d+?","aa2323")) #尽可能少的去匹配\d

  1. import re
  2. s = "this is a number 234-235-22-423"
  3. # 1.贪婪模式
  4. resule = re.match(r"(.+)(\d+-\d+-\d+-\d)",s) #我们本想数字和字母拆解成两个分组
  5. print(resule.groups()) #('this is a number 23', '4-235-22-4')但我们发现输出的结果中23的数字竟然被弄到前面去了
  6. #因为+它会尽可能多的进行匹配,\d,只需要一个4就能满足,所以前面就尽可能多的匹配
  7. # 2.关闭贪婪模式
  8. #在数量词后面加上 ?,进入非贪婪模式,尽可能少的进行匹配
  9. result = re.match(r"(.+?)(\d+-\d+-\d+-\d)",s)
  10. print(result.groups()) #('this is a number ', '234-235-22-4')

案例

匹配手机号

要求,手机号为11位,必须以1开头,且第二个数字为35678其种一个

  1. import re
  2. result = re.match(r'1[35678]\d{9}','13111111111')
  3. print(result.group()) #匹配成功
  4. result = re.match(r'1[35678]\d{9}','12111111111')
  5. print(result) #none,第二位为2
  6. result = re.match(r'1[35678]\d{9}','121111111112')
  7. print(result) #none,有12位

提取网页源码中所有的文字

如下,将其中的所有文字提取出来,去掉标签。思路就是运用sub方法,将标签替换为空

  1. s = """<div>
  2. <p>岗位职责:</p>
  3. <p>完成推荐算法、数据统计、接口、后台等服务器端相关工作</p>
  4. <p><br></p>
  5. <P>必备要求:</p>
  6. <p>良好的自我驱动力和职业素养,工作积极主动、结果导向</p>
  7. <p>&nbsp;<br></p>
  8. <p>技术要求:</p>
  9. <p>1、一年以上 Python开发经验,掌握面向对象分析和设计,了解设计模式</p>
  10. <p>2、掌握HTTP协议,熟悉NVC、MVVM等概念以及相关wEB开发框架</p>
  11. <p>3、掌握关系数据库开发设计,掌握SQL,熟练使用 MySQL/PostgresQL中的一种<br></p>
  12. <p>4、掌握NoSQL、MQ,熟练使用对应技术解决方案</p>
  13. <p>5、熟悉 Javascript/cSS/HTML5,JQuery,React.Vue.js</p>
  14. <p>&nbsp;<br></p>
  15. <p>加分项:</p>
  16. <p>大数据,数理统计,机器学习,sklearn,高性能,大并发。</p>
  17. </div>"""

要提取出来最重要的就是关闭贪婪模式,

  1. result = re.sub(r'<.*?>|&nbsp','',s) #
  2. print(result)

 如果关闭贪婪模式,<xx>中的内容会尽可能多的匹配,只要能够满足后面的>就行,然后<>xxx<>中xxx内容也替换掉了

 提取图片地址

  1. import re
  2. s = """<img data-original="https://img02.sogoucdn.com/app/a/100520024/36189693dc8db6bd7c0be389f8aaddbd.jpg" src="https://img02.sogoucdn.com/app/a/100520024/36189693dc8db6bd7c0be389f8aaddbd.jpg" width="250" height="375" .jpg>"""
  3. result1 = re.search(r"src=\"https.*.jpg\"",s)
  4. print(result1.group())
  5. result2 = re.search(r"src=\"(https.*.jpg)\"",s) #我只是想将网址提取出来,所以httpxx加括号,这样我就可以把它单独提取出来,src则不会出来
  6. print(result2.groups()[0])

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发