# 1 提取出python ''' key = 'javapythonc++php' re.findall('python',key) re.findall('python',key)[0] ''' # 2 提取出 hello word ''' key = '<html><h1>hello word</h1></html>' print(re.findall('<h1>.*</h1>', key)) print(re.findall('<h1>(.*)</h1>', key)) print(re.findall('<h1>(.*)</h1>', key)[0]) ''' # 3 提取170 ''' key = '这个女孩身高170厘米' print(re.findall('\d+', key)[0]) ''' # 4 提取出http://和https:// ''' key = 'http://www.baidu.com and https://www.cnblogs.com' print(re.findall('https?://', key)) ''' # 5 提取出 hello ''' key = 'lalala<hTml>hello</HtMl>hahaha' # 输出的结果<hTml>hello</HtMl> print(re.findall('<[hH][tT][mM][lL]>.*[/hH][tT][mM][lL]>',key)) ''' # 6 提取hit. 贪婪模式;尽可能多的匹配数据 ''' key = 'qiang@hit.edu.com' # 加?是贪婪匹配,不加?是非贪婪匹配 print(re.findall('h.*?\.', key)) ''' # 7 匹配出所有的saas和sas ''' key = 'saas and sas and saaas' print(re.findall('sa{1,2}s',key)) ''' # 8 匹配出 i 开头的行 ''' key = """fall in love with you i love you very much i love she i love her """ print(re.findall('^i.*', key, re.M)) ''' # 9 匹配全部行 ''' key = """ <div>细思极恐 你的队友在看书, 你的闺蜜在减肥, 你的敌人在磨刀, 隔壁老王在练腰. </div> """ print(re.findall('.*', key, re.S)) ''' |
|