day18_ entered Day 18 --re million annual salary of a regular expression

day18

The re module - Regular Expressions

Matching
  • findall (): Find all content from a string and returns a list
s = "meet_宝元_meet"
print(re.findall("meet",s))
# ['meet', 'meet']
  • \ W: Find numbers, letters (Chinese), underscore
  • \ W: Finding non-numeric, alphabetic (Chinese), underscore
s = "meet_宝元_meet123"
print(re.findall("\w",s))
print(re.findall("\W",s))
# ['m', 'e', 'e', 't', '_', '宝', '元', '_', 'm', 'e', 'e', 't', '1', '2', '3']
# []
  • \ S: Find any spaces, line breaks, tabs
  • \ S: Find a non-arbitrary spaces, line breaks, tabs
s = "meet_ 宝元_ meet123    "
print(re.findall("\s",s))
print(re.findall("\S",s))
# [' ', ' ', ' ', ' ', ' ', ' ']
# ['m', 'e', 'e', 't', '_', '宝', '元', '_', 'm', 'e', 'e', 't', '1', '2', '3']
  • \ D: Find figures
  • \ D: Finding non-numeric
s = "meet_ 宝元_ 123    "
print(re.findall("\d",s))
print(re.findall("\D",s))
# ['1', '2', '3']
# ['m', 'e', 'e', 't', '_', ' ', '宝', '元', '_', ' ', ' ', ' ', ' ', ' ']
  • \ A ^ or: Look for what is beginning to
s = "meet_ 宝元_ 123    "
print(re.findall("\Ameet",s))
print(re.findall("^meet",s))
# ['meet']
# ['meet']
  • \ Z or $: end to find out whether what
s = "meet_ 宝元_ 123    tt"
print(re.findall("t\Z",s))
print(re.findall("tt$",s))
# ['t']
# ['tt']
  • \ N: Find newline
s = "meet宝元_123meet \n \t \n"
print(re.findall("\n",s))
# ['\n', '\n']
  • \ T: Find Tabs
s = "meet宝元_123meet \n \t \n"
print(re.findall("\t", s))
# ['\t']
  • : Only match any of the contents (non-newline)
s = "m\net宝元_123maet \n \t "
print(re.findall("m.e", s))
print(re.findall("m.e", s,re.DOTALL))
# ['mae']
# ['m\ne', 'mae']
  • []: Matching characters in a character set
s = "meet宝元_1A-23maet"
print(re.findall("[a-z]", s))  # 小写的a-z
print(re.findall("[A-Z]", s))  # 大写的A-Z
print(re.findall("[a-zA-Z]", s))  # 大小写的20字母
print(re.findall("[a-zA-Z0-9]", s))  # 大小写的20字母,0-9
print(re.findall("[^0-9]", s))  # 查找非0-9的内容
# ['m', 'e', 'e', 't', 'm', 'a', 'e', 't']
# ['A']
# ['m', 'e', 'e', 't', 'A', 'm', 'a', 'e', 't']
# ['m', 'e', 'e', 't', '1', 'A', '2', '3', 'm', 'a', 'e', 't']
# ['m', 'e', 'e', 't', '宝', '元', '_', 'A', '-', 'm', 'a', 'e', 't']
  • *: 0 or more matches [greedy matches]
s = "m mm mmmmmm"
print(re.findall("m*", s))
print(re.findall("m*m", s))
# ['m', '', 'mm', '', 'mmmmmm', '']
# ['m', 'mm', 'mmmmmm']
  • +: 1 or more Match [greedy matches]
s = "m mm mmmmmm"
print(re.findall("m+", s))
print(re.findall("m+m", s))
# ['m', 'mm', 'mmmmmm']
# ['mm', 'mmmmmm']
  • ?: Matches zero or one [non-greedy matches]
s = "m mm mmmmmm"
print(re.findall("m?",s))
# ['m', '', 'm', 'm', '', 'm', 'm', 'm', 'm', 'm', 'm', '']
  • SUMMARY {n}: Find what repeated n times
s = "m mm mmm mmmm mmmmm mmmmmm"
print(re.findall("m{2}", s))
print(re.findall("m{4}", s))
print(re.findall("m{6}", s))
# ['mm', 'mm', 'mm', 'mm', 'mm', 'mm', 'mm', 'mm', 'mm']
# ['mmmm', 'mmmm', 'mmmm']
# ['mmmmmm']
  • SUMMARY {n, m}: Find what times repeated nm
s = "m mm mmm mmmm mmmmm mmmmmm"
print(re.findall("m{2,5}", s))
# ['mm', 'mmm', 'mmmm', 'mmmmm', 'mmmmm']
  • a | b: or
s = "m mm mmss ss s"
print(re.findall("m|s", s))
# ['m', 'm', 'm', 'm', 'm', 's', 's', 's', 's', 's']
  • (): Matching expression in brackets, also represents a group
s = "meet_assdf_mssst_(.)mmns_aaamaaatmsssssssssssstt"
print(re.findall("m(..)t", s))
print(re.findall("m(.?)t", s))
print(re.findall("m(..?)t", s))
print(re.findall("m(?:..?)t",s))  # ?:m和t都连上
print(re.findall("m(.*)t", s))
print(re.findall("m(.*?)t", s))
print(re.findall("m(.+)t", s))
print(re.findall("m(.+?)t", s))
# ['ee']
# []
# ['ee']
# ['meet']
# ['eet_assdf_mssst_(.)mmns_aaamaaatmsssssssssssst']
# ['ee', 'sss', 'mns_aaamaaa', 'ssssssssssss']
# ['eet_assdf_mssst_(.)mmns_aaamaaatmsssssssssssst']
# ['ee', 'sss', 'mns_aaamaaa', 'ssssssssssss']

print(re.search("(?P<tag_name>\w+)\w+\w+","h1hellh1").group())
print(re.search("(?P<aaa>\w+)dfa","asbsadfasdfa").group("aaa"))
print(re.search("(?P<cx>\w+)dfa","asbsadfasdfa").group())
# h1hellh1
# asbsadfas   # ?P:命名
# asbsadfasdfa

s = 'alex_sb ale123_sb wu12sir_sb wusir_sb ritian_sb 的 alex wusir '
print(re.findall("\w+_sb",s))
print(re.findall("[a-z]+_sb",s))
# ['alex_sb', 'ale123_sb', 'wu12sir_sb', 'wusir_sb', 'ritian_sb']
# ['alex_sb', 'sir_sb', 'wusir_sb', 'ritian_sb']
method
  • search (): find a stop after the search, to find the string, after finding an object is returned, view the elements (group ())
s = '_sb alex 123_sb wu12sir_sb wusir_sb ritian_sb 的 x wusir '
print(re.search("ale",s)) # 如果不存在返回None
print(re.search("ale",s).group()) # 如果不存在报错
# <re.Match object; span=(4, 7), match='ale'>
# ale
  • match (): stop the search after finding a look only from the beginning of the string, returns an object after find, view elements (group ())
s = 'ale_sb alex 123_sb wu12sir_sb wusir_sb ritian_sb 的 x wusir '
print(re.match("ale",s))  # 开头找不到就返回None
print(re.match("ale",s).group()) # 开头找不到就报错
# <re.Match object; span=(0, 3), match='ale'>
# ale
  • split (): split
s = '_sb alex,123:sb;wu12sir#sb*wusir!sb ritian_sb 的 x wusir '
print(re.split("[#,:!*]",s))
# ['_sb alex', '123', 'sb;wu12sir', 'sb', 'wusir', 'sb ritian_sb 的 x wusir ']
  • sub (): Replace
print(re.sub("barry","宝元",'barry是最好的讲师,barry就是一个普通老师,请不要将barry当男神对待。'))
# 宝元是最好的讲师,宝元就是一个普通老师,请不要将宝元当男神对待。
  • compile (): defined matching rules
obj = re.compile("\w")
print(obj.findall("meet_宝元_阿萨大大"))
print(re.findall("\w","meet_宝元_阿萨大大"))
# ['m', 'e', 'e', 't', '_', '宝', '元', '_', '阿', '萨', '大', '大']
# ['m', 'e', 'e', 't', '_', '宝', '元', '_', '阿', '萨', '大', '大']
  • finditer (): Returns an iterator address
g = re.finditer("\w","大大撒旦")
# print(next(g).group())
for i in g:
    print(i.group())
# 大
# 大
# 撒
# 旦
Exercise
1、取整数
s = "1-2*(60+(-40.35/5)-(-4*3))"
print(re.findall("\d+",s))
# ['1', '2', '60', '40', '35', '5', '4', '3']

1.2 匹配所有的数字(包含小数)
s = "1-2*(60+(-40.35/5)-(-4*3))"
print(re.findall("\d+\.\d+|\d+",s))
# ['1', '2', '60', '40.35', '5', '4', '3']

匹配所有的数字(包含小数包含负号)
s = "1-2*(60+(-40.35/5)-(-4*3))"
print(re.findall("-?\d+\.\d+|-?\d+",s))
# ['1', '-2', '60', '-40.35', '5', '-4', '3']

2,匹配一段你文本中的每行的邮箱
http://blog.csdn.net/make164492212/article/details/51656638 匹配所有邮箱
s = "http://blog.csdn.net/make164492212/article/details/51656638 匹配所有邮箱"
print(re.findall("h(?:.+)ls/", s))
# ['http://blog.csdn.net/make164492212/article/details/']

3,匹配一段你文本中的每行的时间字符串 这样的形式:'1995-04-27'
s1 = '''
时间就是1995-04-27,2005-04-27
1999-04-27 老男孩教育创始人
老男孩老师 alex 1980-04-27:1980-04-27
2018-12-08
'''
print(re.findall("\d+-\d+-\d+",s1))
# ['1995-04-27', '2005-04-27', '1999-04-27', '1980-04-27', '1980-04-27', '2018-12-08']

4、匹配qq号:腾讯从10000开始:
num = input("请输入你的数字:")
print(re.findall("[1-9][0-9]{5,10}",num))
# 请输入你的数字:1719932187171993218
# ['1719932187', '171993218']

s1 = '''
<div id="cnblogs_post_body" class="blogpost-body"><h3><span style="font-family: 楷体;">python基础篇</span></h3>
<p><span style="font-family: 楷体;">&nbsp; &nbsp;<strong><a href="http://www.cnblogs.com/guobaoyuan/p/6847032.html" target="_blank">python 基础知识</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/p/6627631.html" target="_blank">python 初始python</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<strong><a href="http://www.cnblogs.com/guobaoyuan/articles/7087609.html" target="_blank">python 字符编码</a></strong></strong></span></p>
<p><span style="font-family: 楷体;"><strong><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/6752157.html" target="_blank">python 类型及变量</a></strong></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/p/6847663.html" target="_blank">python 字符串详解</a></strong></span></p>
<p><span style="font-family: 楷体;">&nbsp; &nbsp;<strong><a href="http://www.cnblogs.com/guobaoyuan/p/6850347.html" target="_blank">python 列表详解</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/p/6850496.html" target="_blank">python 数字元祖</a></strong></span></p>
<p><span style="font-family: 楷体;">&nbsp; &nbsp;<strong><a href="http://www.cnblogs.com/guobaoyuan/p/6851820.html" target="_blank">python 字典详解</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<strong><a href="http://www.cnblogs.com/guobaoyuan/p/6852131.html" target="_blank">python 集合详解</a></strong></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/7087614.html" target="_blank">python 数据类型</a>&nbsp;</strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/p/6752169.html" target="_blank">python文件操作</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/p/8149209.html" target="_blank">python 闭包</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/6705714.html" target="_blank">python 函数详解</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/7087616.html" target="_blank">python 函数、装饰器、内置函数</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/7087629.html" target="_blank">python 迭代器 生成器</a>&nbsp;&nbsp;</strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/6757215.html" target="_blank">python匿名函数、内置函数</a></strong></span></p>
</div>
'''
1,找到所有的span标签的内容
ret = re.findall('<span(.*?)>', s1)
print(ret)

print(re.findall('<a href="(.*?)"',s1))

Guess you like

Origin www.cnblogs.com/NiceSnake/p/11284928.html