20-正则表达式

lesson20正则表达式
#视频1:后续安排

------------------------------------------------------------------------
#视频2 包和包的管理

#知识点:
相对路径导入只能被调用,不能运行---------- from .tt22 import func_tt22 # 点代表当前目录 ,点点代表上层目录 cd ..

 1 #正则表达式的作用1
 2 st='''
 3  <p>1.根据业务需求制定系统的整体技术框架,承担从业务向技术转换的桥梁作用;</p><p>2.负责公司系统的技术选型、架构设计、解决方案研发,对系统的可扩展性、安全性等做系统级的控制;</p><p>3.负责组织技术研究和攻关工作,解决系统开发、 运行中出现的各种问题;</p><p>4.负责系统框架相关技术和业务进行培训,指导开发人员开发。</p><p>岗位要求:</p><p>1.全日制本科及以上学历,计算机相关专业;具有3年以上 C/S 或 B/S 体系结构软件产品开发及架构和设计经验;</p><p>2.具备丰富的大中型开发项目的总体规划、方案设计及技术队伍管理经验,有大中型应用系统开发和实施的成功案例;</p><p>3.对JAVA技术及整个解决方案有深刻的理解,精通微服务和分布式架构设计;</p><p>4.熟悉数据库如 Oracle、Mysql等的开发;熟悉中间件如Redis、Nginx、ES等的开发;熟悉各类设计模式的实现;</p><p>5.熟悉Linux开发环境,熟练配置维护Nginx、Tomcat、Zookeeper等服务,掌握shell脚本工具。</p>
 4 '''
 5 import re
 6 
 7 result = re.findall(r'<p>(.+?)</p>',st)
 8 
 9 
10 for i in result:
11     print(i)

#运行:
1.根据业务需求制定系统的整体技术框架,承担从业务向技术转换的桥梁作用;
2.负责公司系统的技术选型、架构设计、解决方案研发,对系统的可扩展性、安全性等做系统级的控制;
3.负责组织技术研究和攻关工作,解决系统开发、 运行中出现的各种问题;
4.负责系统框架相关技术和业务进行培训,指导开发人员开发。
岗位要求:
1.全日制本科及以上学历,计算机相关专业;具有3年以上 C/S 或 B/S 体系结构软件产品开发及架构和设计经验;
2.具备丰富的大中型开发项目的总体规划、方案设计及技术队伍管理经验,有大中型应用系统开发和实施的成功案例;
3.对JAVA技术及整个解决方案有深刻的理解,精通微服务和分布式架构设计;
4.熟悉数据库如 Oracle、Mysql等的开发;熟悉中间件如Redis、Nginx、ES等的开发;熟悉各类设计模式的实现;
5.熟悉Linux开发环境,熟练配置维护Nginx、Tomcat、Zookeeper等服务,掌握shell脚本工具。

------------------------------------------------------------------------
#视频3 正则的作用
#正则学习2 可以在xshell中测试

 1 #正则学习2
 2 a=re.findall(r'py','python3333pyttttt')    #返回列表,没有返回空
 3 print(a)    #['py', 'py']
 4 
 5 s=re.match('py','python22222222222pytttttt')   #必须从第一个字符开始找
 6 print(s)        #符合规则则返回
 7 print(s.span())     #(0, 2)     返回下标区间
 8 print(s.group())    #py     返回数据
 9 
10 t=re.search('ython','python22222222222pytttttt')  #从内容中随意找
11 print(t)
12 print(t.span())
13 print(t.group())

#运行:
['py', 'py']
<_sre.SRE_Match object; span=(0, 2), match='py'>
(0, 2)
py
<_sre.SRE_Match object; span=(1, 6), match='ython'>
(1, 6)
ython


------------------------------------------------------------------------
#视频4 元字符1
3分钟:xshell操作练习:
>>> import re
>>> re.findall(r'.','aassdd') # . 匹配任意1个字符,除了\n
['a', 'a', 's', 's', 'd', 'd']
>>> re.findall(r'a.','aassdd')
['aa']
>>> re.findall(r's.','aassdd')
['ss']
>>> re.findall(r'd.','aassdd')
['dd']
>>> re.findall(r'as.','aassdd')
['ass']
>>> re.findall(r'[a]','aassdd') #匹配[]中列举的字符
['a', 'a']
>>> re.findall(r'[a,b,s]','aassdd')
['a', 'a', 's', 's']
>>> re.findall(r'[a-z]','aassdd')
['a', 'a', 's', 's', 'd', 'd']
>>> re.findall(r'[A-Z]','aassdd')
[]
>>> re.findall(r'[0-9]','aassdd121122')
['1', '2', '1', '1', '2', '2']
>>> re.findall(r'\d','aassdd121122') #\d 匹配所有数字
['1', '2', '1', '1', '2', '2']
>>> re.findall(r'\D','aassdd121122') #\D 匹配所有非数字
['a', 'a', 's', 's', 'd', 'd']
>>> re.findall(r'\s','aass dd 121122!@') # \s 匹配空格
[' ', ' ']
>>> re.findall(r'\S','aass dd 121122!@') # \s 匹配所有非空格的数据
['a', 'a', 's', 's', 'd', 'd', '1', '2', '1', '1', '2', '2', '!', '@']

>>> re.findall(r'\w','aass dd 121122!@_') # w匹配数字,字母 下划线
['a', 'a', 's', 's', 'd', 'd', '1', '2', '1', '1', '2', '2', '_']
>>> re.findall(r'\W','aass dd 121122!@') # W 匹配非单词字符
[' ', ' ', '!', '@']

#配合 * + ? {m} {m,} {m,n}
>>> re.findall(r'\d{11}','aass dd 121asdf=+ ss12312312122!@_') #匹配11个数字
['12312312122']
>>> re.findall(r'\d{10,}','aass dd 121asdf=+ ss12312312122!@_') #匹配10次以上数字
['12312312122']
>>> re.findall(r'\d{2,5}','aass dd 121asdf=+ ss12312312122!@_') #匹配2-5次数字
['121', '12312', '31212']

>>> re.findall(r'\d*','aass dd 121asdf=+ ss12312312122!@_') # *匹配包括0次数字
['', '', '', '', '', '', '', '', '121', '', '', '', '', '', '', '', '', '', '12312312122', '', '', '', '']
>>> re.findall(r'\d+','aass dd 121asdf=+ ss12312312122!@_') # +匹配起码1次以上数字
['121', '12312312122']
>>> re.findall(r'\d\d?','aass dd 121asdf=+ ss12312312122!@_') # ?匹配一个字符出现1次或0次,即要么1次,要么没有
['12', '1', '12', '31', '23', '12', '12', '2']
>>> re.findall(r'\d\d*','aass dd 121asdf=+ ss12312312122!@_') #匹配1次或以上都可以
['121', '12312312122']
>>> re.findall(r'\d\d+','aass dd 121asdf=+ ss12312312122!@_') #匹配2次以上都可以
['121', '12312312122']


---------------------------------------------------------------------
#正则匹配,代码:

 1 #正则匹配
 2 import re
 3 
 4 a=re.findall(r'.','python3333pyttttt')  # . 表示任意字符
 5 print(a)
 6 
 7 a=re.findall(r'\d','python3333pyttttt')  #\d匹配数字,0-9
 8 print(a)
 9 
10 a=re.findall(r'\D','python3333pyttttt')  # \D匹配非数字
11 print(a)
12 
13 a=re.findall(r'\s','py thon3333py ttttt')  # \s匹配空格  ,即空格,tab
14 print(a)
15 
16 a=re.findall(r'\S','py thon3333py ttttt')  # \S匹配非空格
17 print(a)
18 
19 a=re.findall(r'\w','py thon3333py! ttttt')  # \w匹配单词字符,即数字,字母,下划线
20 print(a)
21 
22 
23 a=re.findall(r'2\d{3}','py thon2333py! ttttt?')  # 匹配2打头后面再加3个3
24 print(a)
25 
26 a=re.findall(r'\d{3,10}','py thon2333433545py123123! ttttt?')  # 匹配3-10个数字
27 print(a)
28 
29 a=re.findall(r'\d*','py thon2333433545py123123! ttttt?')  # 匹配
30 print(a)
31 
32 a=re.findall(r'\d+','py thon2333433545py123123! ttttt?')  # 匹配出现1次或无限次
33 print(a)
34 
35 a=re.findall(r'\d\d?','python2333433545py123123ttttt')  # 匹配出现1次或0此
36 print(a)


#运行:
['p', 'y', 't', 'h', 'o', 'n', '3', '3', '3', '3', 'p', 'y', 't', 't', 't', 't', 't']
['3', '3', '3', '3']
['p', 'y', 't', 'h', 'o', 'n', 'p', 'y', 't', 't', 't', 't', 't']
[' ', ' ']
['p', 'y', 't', 'h', 'o', 'n', '3', '3', '3', '3', 'p', 'y', 't', 't', 't', 't', 't']
['p', 'y', 't', 'h', 'o', 'n', '3', '3', '3', '3', 'p', 'y', 't', 't', 't', 't', 't']
['2333']
['2333433545', '123123']
['', '', '', '', '', '', '', '2333433545', '', '', '123123', '', '', '', '', '', '', '', '', '']
['2333433545', '123123']
[]


------------------------------------------------------------------------
#视频5 元字符2
4,xshell操作练习:

>>> str1='''
... python123
... pytho111
... 111222pytho999
... aaaa777777
... abcd1234python
... '''
>>> re.findall(r'python',str1) #完全匹配
['python', 'python']
>>> re.findall(r'\bpython',str1) #匹配单词前面有边界 ,边界是空格
['python']
>>> re.findall(r'\Bpython',str1) #匹配非单词边界
['python']
>>>
>>> re.findall(r'\bpyt\dhon','llpyt1hon pyt2honrr pyt3hon ') # 匹配单词前有边界
['pyt2hon', 'pyt3hon']
>>> re.findall(r'\bpyt\dhon\B','llpyt1hon pyt2honrr pyt3hon ') #匹配单词前后都有边界
['pyt2hon']
>>>
>>> re.findall(r'\bpyt\dhon\b','llpyt1hon pyt2honrr pyt3hon ') #
['pyt3hon']
>>> re.findall(r'\Bpyt\dhon\B','llpyt1hon pyt2honrr pyt3hon ')
[]

>>> re.findall(r'^py','python') # ^匹配 py开头的单词
['py']
>>> re.findall(r'^python','python')
['python']
>>> re.findall(r'^thon','python')
[]
>>> re.findall(r'thon$','python') # $ 匹配thon结尾的单词
['thon']

>>> re.findall(r'^\D{6}$','python') #匹配6个字母单词
['python']
>>> re.findall(r'^\D{5}$','python')
[]
>>> re.findall(r'^\D+$','python') #匹配1次以上字母单词
['python']
>>> re.findall(r'^\D+\d+$','python11111') #匹配1个以上字母,1个以上数字的单词
['python11111']
>>> re.findall(r'^\w+$','python11111') #匹配1个以上字母,数字,下划线的单词
['python11111']

#17分钟;分组匹配
>>> re.findall(r'\D+|\d+','aaaa111bbbb111') # | 代表or或 ,匹配1次以上字母或数字
['aaaa', '111', 'bbbb', '111']

--------------------------------------

代码:

 1 str1='''
 2 python123
 3 pytho111
 4 111222pytho999
 5 aaaa777777
 6 abcd1234python
 7 
 8 '''
 9 import  re
10 a=re.findall(r'python',str1) #完全匹配
11 print(a)
12 
13 a=re.findall(r'^pyth',str1)
14 print(a)
15 
16 a=re.findall(r'\bpython',str1) #匹配前面没有
17 print(a)
18 
19 a=re.findall(r'\Bpython',str1)  #匹配前面有单词
20 print(a)
21 
22 a=re.findall(r'\Bpython\b',str1)
23 print(a)
24 
25 
26 #---------------------------------------
27 print('--------------------------------------------------')
28 st ='''
29 <p>职位</p><p>描述: </p>
30 
31 '''
32 
33 s=result =re.findall(r'<p>.*?</p>',st)
34 print(s)
35 
36 s=result =re.findall(r'<p>(.*?)</p>',st)  #格式控制分组()    ['职位', '描述: ']
37 print(s)
38 
39 s=result =re.findall(r'(<p>).*?(</p>)',st)  #格式控制分组()
40 print(s)
41 
42 
43 ar='''
44 <span><p>tanzhou</p></span>
45 
46 '''
47 
48 result =re.findall(r'<(span)><(p)>.+?<(/\2)><(/\1)>',ar)  #\2 和 \1是引用 span p
49 print(result)
50 
51 result =re.findall(r'<(.+)><(.+)>.+?<(/\2)><(/\1)>',ar)  #\2 和 \1是引用 span p
52 print(result)


#运行:
['python', 'python']
[]
['python']
['python']
['python']
['<p>职位</p>', '<p>描述: </p>']
['职位', '描述: ']
[('<p>', '</p>'), ('<p>', '</p>')]
[('span', 'p', '/p', '/span')]
[('span', 'p', '/p', '/span')]


------------------------------------------------------------
#备注: + * {m,} {m,n} 这4中是贪婪模式
>>> re.findall(r'\d+','aaaa111bbbb111') # +和*贪婪模式
['111', '111']
>>> re.findall(r'\d+?','aaaa111bbbb111') # ?关闭贪婪模式
['1', '1', '1', '1', '1', '1']
>>> re.findall(r'\d{2,}','aaaa111bbbb111cc333333333333')
['111', '111', '333333333333']
>>> re.findall(r'\d{2,3}','aaaa111bbbb111cc333333333333')
['111', '111', '333', '333', '333', '333']
>>>

猜你喜欢

转载自www.cnblogs.com/tiantiancode/p/12899890.html
今日推荐