按下面要求编写正则表达式
-
电子邮箱验证
import re def validateEmail(email): if len(email) > 7: if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None: return 1 return 0 # 测试与结果 In [47]: validateEmail('example@gmail.com') Out[47]: 1 In [48]: validateEmail('example@g.N') Out[48]: 0 In [49]: validateEmail('example@sina.com.cn') Out[49]: 1 In [50]: validateEmail('example@sina.net.hh') Out[50]: 1
-
URL地址验证
regex = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain... r'localhost|' #localhost... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) # 测试与结果 regex.match('http://bbs.smartisan.com/thread-107969-1-1.html') regex.match('http://www.spring.org.uk/') regex.match('http://www.pixiv.net/member_illust.php?mode=manga&illust_id=60181180') regex.match('https://www.google.com/ 886')
-
查找HTML代码中的<img>标签内容
findImg = re.compile(r'<img[^>]+>') htmls = '<div class="ranking-image-item"><a href="<div class="contents-main"><div class="ads-top-info"></div> <div class="rounded"><div class="NewsTop"><h1><a href="info.php" title="お知らせ">お知らせ</a></h1><ul class="top-info-content"><li class="info_rb" style="width:527px;"><a href="info.php?cid=1" class="category _1">お知らせ</a> <a href="info.php?id=3897"><span class="title">【重要】pixivの一部アカウントに対する「なりすましログイン」の報告とパスワード変更のお願い</span></a><span class="date">12月2日</span><div><div class="hideButton"></div></div></li><li class="info" style="width:527px;"><a href="info.php?cid=7" class="category _7">ギャラリー</a> <a href="info.php?id=3889"><span class="title">pixiv Zingaroにて「SNOW MIKU東京展2017」1月12日より開催</span></a><span class="date">12月2日</span><div><div class="hideButton"><a href="javascript:void(0);" class="remove" onclick="pixiv.hideTopInfo.hide(this)" data-infoid="3889">×</a></div></div></li><li class="info" style="width:527px;"><a href="info.php?cid=3" class="category _3">新機能</a> <a href="info.php?id=3887"><span class="title">クリエイターとファンをつなぐコンテンツプラットフォーム「pixivFANBOX」をリリース</span></a><span class="date">12月1日</span><div><div class="hideButton"><a href="javascript:void(0);" class="remove" onclick="pixiv.hideTopInfo.hide(this)" data-infoid="3887">×</a><div class="_layout-thumbnail">![](http://i1.pixiv.net/c/150x150/img-master/img/2016/12/03/15/18/51/60214620_p0_master1200.jpg)</div></a><a href="/member_illust.php?mode=medium&illust_id=60214620&uarea=new_illust"><h1 class="title gtm-everyone-new-illusts" title="こちらイシガメ鎮守布">こちらイシガメ鎮守布</h1></a><a href="/member_illust.php?id=3247146" class="user ui-profile-popup gtm-everyone-new-illusts" title="ハヌマ~ン" data-user_id="3247146" data-user_name="ハヌマ~ン">ハヌマ~ン</a></li><li class="image-item"><a href="/member_illust.php?mode=medium&illust_id=60214618&uarea=new_illust" class="work _work "><div class="_layout-thumbnail">![](http://i3.pixiv.net/c/150x150/img-master/img/2016/12/03/15/18/41/60214618_p0_master1200.jpg)</div><div class="_layout-thumbnail">![](http://i2.pixiv.net/c/150x150/img-master/img/2016/12/03/15/18/39/60214617_p0_master1200.jpg)</div><a href="/member_illust.php?id=16622479" class="user ui-profile-popup gtm-everyone-new-illusts" title="Solu" data-user_id="16622479" data-user_name="Solu">Solu</a></li><li class="image-item"><a href="/member_illust.php?mode=medium&illust_id=60214614&uarea=new_illust" class="work _work multiple "><div class="_layout-thumbnail">![](http://i3.pixiv.net/c/150x150/img-master/img/2016/12/03/15/17/53/60214614_p0_master1200.jpg)<div class="_layout-thumbnail">![](http://i2.pixiv.net/c/150x150/img-master/img/2016/12/03/15/17/52/60214613_p0_master1200.jpg)</div>![](http://i4.pixiv.net/c/150x150/img-master/img/2016/12/03/15/17/33/60214611_p0_master1200.jpg)</div></a><a href="/member_illust.php?mode=medium&illust_id=60214611&uarea=new_illust"><h1 class="title gtm-everyone-new-illusts" title="もどかしさ。">もどかしさ。</h1></a> ![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/292213/9965b565-0c8c-4aa8-b652-1bf84510b48c_f_150x150.jpg)</div><h1 class="title" title="マスキングテープ Illustrated by majocco">マスキングテープ Illustrated by ...</h1></a><a href="/member_illust.php?id=11" class="user ui-profile-popup" title="pixiv事務局" data-user_id="11" data-user_name="pixiv事務局">pixiv事務局</a></li><li class="image-item"><a href="https://pixiv.booth.pm/items/292178?utm_source=pixiv&utm_medium=mypage&utm_content=follow-item&utm_campaign=pixiv-follow-items" class="work" target="_blank"><div class="_layout-thumbnail">![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/292178/f0945a3f-eec6-4200-b0f6-b04803453312_f_150x150.jpg)</div><h1 class="title" title="マスキングテープ Illustrated by 岬">マスキングテープ Illustrated by ...</h1></a><a href="/member_illust.php?id=11" class="user ui-profile-popup" title="pixiv事務局" data-user_id="11" data-user_name="pixiv事務局">pixiv事務局</a></li><li class="image-item"><a href="https://pixiv.booth.pm/items/267365?utm_source=pixiv&utm_medium=mypage&utm_content=follow-item&utm_campaign=pixiv-follow-items" class="work" target="_blank"><div class="_layout-thumbnail">![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/267365/93f192fc-ae83-4e98-bff5-096826bcb0c4_f_150x150.jpg)</div><h1 class="title" title="「あしょんでよッ~うちの犬ログ~」マグカップ">「あしょんでよッ~うちの犬ログ~」マグカップ</h1></a><a href="/member_illust.php?id=11" class="user ui-profile-popup" title="pixiv事務局" data-user_id="11" data-user_name="pixiv事務局">pixiv事務局</a></li><li class="image-item"><a href="https://pixiv.booth.pm/items/267373?utm_source=pixiv&utm_medium=mypage&utm_content=follow-item&utm_campaign=pixiv-follow-items" class="work" target="_blank"><div class="_layout-thumbnail">![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/267373/89474534-f844-4ff7-8dc0-9016dc46d4a7_f_150x150.jpg)</div><h1 class="title" title="「そうしそうあい」トートバッグ">「そうしそうあい」トートバッグ</h1></a><a href="/member_illust.php?id=11" class="user ui-profile-popup" title="pixiv事務局" data-user_id="11" data-user_name="pixiv事務局">pixiv事務局</a></li><li class="image-item"><a href="https://pixiv.booth.pm/items/267356?utm_source=pixiv&utm_medium=mypage&utm_content=follow-item&utm_campaign=pixiv-follow-items" class="work" target="_blank"><div class="_layout-thumbnail">![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/267356/99649852-40e6-456e-8979-f464f917e5ce_f_150x150.jpg)</div><li class="image-item"><a href="https://pixiv.booth.pm/items/267359?utm_source=pixiv&utm_medium=mypage&utm_content=follow-item&utm_campaign=pixiv-follow-items" class="work" target="_blank"><div class="_layout-thumbnail">![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/267359/b64a023b-8ff3-4881-ac1d-52eae1b6bf54_f_150x150.jpg)</div> <p class="registration">まだBOOTHにショップがない方は<a href="https://booth.pm/start?utm_source=pixiv&utm_medium=mypage&utm_content=registration&utm_campaign=pixiv-follow-items" target="_blank">こちらから登録</a></p></div></section></section></div></div>" data-filter="lazy-image"></div></a></div>' findImg.findall(htmls) # 结果输出 Out[3]: ['![](http://i1.pixiv.net/c/150x150/img-master/img/2016/12/03/15/18/51/60214620_p0_master1200.jpg)', '![](http://i3.pixiv.net/c/150x150/img-master/img/2016/12/03/15/18/41/60214618_p0_master1200.jpg)', '![](http://i2.pixiv.net/c/150x150/img-master/img/2016/12/03/15/18/39/60214617_p0_master1200.jpg)', '![](http://i3.pixiv.net/c/150x150/img-master/img/2016/12/03/15/17/53/60214614_p0_master1200.jpg)', '![](http://i2.pixiv.net/c/150x150/img-master/img/2016/12/03/15/17/52/60214613_p0_master1200.jpg)', '![](http://i4.pixiv.net/c/150x150/img-master/img/2016/12/03/15/17/33/60214611_p0_master1200.jpg)', '![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/292213/9965b565-0c8c-4aa8-b652-1bf84510b48c_f_150x150.jpg)', '![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/292178/f0945a3f-eec6-4200-b0f6-b04803453312_f_150x150.jpg)', '![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/267365/93f192fc-ae83-4e98-bff5-096826bcb0c4_f_150x150.jpg)', '![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/267373/89474534-f844-4ff7-8dc0-9016dc46d4a7_f_150x150.jpg)', '![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/267356/99649852-40e6-456e-8979-f464f917e5ce_f_150x150.jpg)', '![](https://s.booth.pm/77c6444b-e8fa-46d9-a5e2-6463c889a13b/i/267359/b64a023b-8ff3-4881-ac1d-52eae1b6bf54_f_150x150.jpg)']
-
匹配正整数
findPositiveInt = re.compile('^ [1-9]\d*$') stringI = ['123', '3', '-453', 'jid 5', '2.345', '-432','34', '5321'] for i in stringI: print(findPositiveInt.findall(i)) # 结果输出 ['123'] ['3'] [] [] [] [] ['34'] ['5321']