按下面要求编写正则表达式
-
电子邮箱验证
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"></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"></div><div class="_layout-thumbnail"></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"><div class="_layout-thumbnail"></div></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> </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"></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"></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"></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"></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"></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]: ['', '', '', '', '', '', '', '', '', '', '', '']
-
匹配正整数
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']