{"id":591,"date":"2020-02-10T22:41:27","date_gmt":"2020-02-10T14:41:27","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/wordpress\/?p=591"},"modified":"2020-02-10T22:41:27","modified_gmt":"2020-02-10T14:41:27","slug":"%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%ef%bc%88python%ef%bc%89","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=591","title":{"rendered":"\u6b63\u5219\u8868\u8fbe\u5f0f\uff08python\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. \u5b9e\u4f8b\u5f15\u5165<\/h2>\n\n\n\n<p>\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u53ef\u80fd\u6211\u4eec\u5bf9\u5b83\u5230\u5e95\u662f\u4e2a\u4ec0\u4e48\u8fd8\u662f\u6bd4\u8f83\u6a21\u7cca\uff0c\u4e0b\u9762\u5c31\u7528\u51e0\u4e2a\u5b9e\u4f8b\u6765\u770b\u4e00\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<p>\u6253\u5f00\u5f00\u6e90\u4e2d\u56fd\u63d0\u4f9b\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6d4b\u8bd5\u5de5\u5177<a href=\"http:\/\/tool.oschina.net\/regex\/\">http:\/\/tool.oschina.net\/regex\/<\/a>\uff0c\u8f93\u5165\u5f85\u5339\u914d\u7684\u6587\u672c\uff0c\u7136\u540e\u9009\u62e9\u5e38\u7528\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5c31\u53ef\u4ee5\u5f97\u51fa\u76f8\u5e94\u7684\u5339\u914d\u7ed3\u679c\u4e86\u3002\u4f8b\u5982\uff0c\u8fd9\u91cc\u8f93\u5165\u5f85\u5339\u914d\u7684\u6587\u672c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\tHello, my phone number is 010-86432100 and email is cqc@cuiqingcai.com, and my website is http:\/\/cuiqingcai.com.\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>Hello, my phone number is 010-86432100 and email is cqc@cuiqingcai.com, and my website is http:\/\/cuiqingcai.com.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u6bb5\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a\u7535\u8bdd\u53f7\u7801\u548c\u4e00\u4e2a\u7535\u5b50\u90ae\u4ef6\uff0c\u63a5\u4e0b\u6765\u5c31\u5c1d\u8bd5\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u51fa\u6765\uff0c\u5982\u56fe3-10\u6240\u793a\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/qiniu.cuiqingcai.com\/wp-content\/uploads\/2018\/02\/3-10.jpg\" alt=\"\" class=\"wp-image-5531\"\/><\/figure><\/div>\n\n\n\n<p>\u56fe3-10 \u8fd0\u884c\u9875\u9762<\/p>\n\n\n\n<p>\u5728\u7f51\u9875\u53f3\u4fa7\u9009\u62e9\u201c\u5339\u914dEmail\u5730\u5740\u201d\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u4e0b\u65b9\u51fa\u73b0\u4e86\u6587\u672c\u4e2d\u7684E-mail\u3002\u5982\u679c\u9009\u62e9\u201c\u5339\u914d\u7f51\u5740URL\u201d\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u4e0b\u65b9\u51fa\u73b0\u4e86\u6587\u672c\u4e2d\u7684URL\u3002\u662f\u4e0d\u662f\u975e\u5e38\u795e\u5947\uff1f<\/p>\n\n\n\n<p>\u5176\u5b9e\uff0c\u8fd9\u91cc\u5c31\u662f\u7528\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff0c\u4e5f\u5c31\u662f\u7528\u4e00\u5b9a\u7684\u89c4\u5219\u5c06\u7279\u5b9a\u7684\u6587\u672c\u63d0\u53d6\u51fa\u6765\u3002\u6bd4\u5982\uff0c\u7535\u5b50\u90ae\u4ef6\u5f00\u5934\u662f\u4e00\u6bb5\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u662f\u4e00\u4e2a@\u7b26\u53f7\uff0c\u6700\u540e\u662f\u67d0\u4e2a\u57df\u540d\uff0c\u8fd9\u662f\u6709\u7279\u5b9a\u7684\u7ec4\u6210\u683c\u5f0f\u7684\u3002\u53e6\u5916\uff0c\u5bf9\u4e8eURL\uff0c\u5f00\u5934\u662f\u534f\u8bae\u7c7b\u578b\uff0c\u7136\u540e\u662f\u5192\u53f7\u52a0\u53cc\u659c\u7ebf\uff0c\u6700\u540e\u662f\u57df\u540d\u52a0\u8def\u5f84\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8eURL\u6765\u8bf4\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t[a-zA-z]+:\/\/[^\\s]*\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>[a-zA-z]+:\/\/[^\\s]*<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7528\u8fd9\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u5339\u914d\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7c7b\u4f3cURL\u7684\u6587\u672c\uff0c\u90a3\u5c31\u4f1a\u88ab\u63d0\u53d6\u51fa\u6765\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u770b\u4e0a\u53bb\u662f\u4e71\u7cdf\u7cdf\u7684\u4e00\u56e2\uff0c\u5176\u5b9e\u4e0d\u7136\uff0c\u8fd9\u91cc\u9762\u90fd\u662f\u6709\u7279\u5b9a\u7684\u8bed\u6cd5\u89c4\u5219\u7684\u3002\u6bd4\u5982\uff0c<code>a-z<\/code>\u4ee3\u8868\u5339\u914d\u4efb\u610f\u7684\u5c0f\u5199\u5b57\u6bcd\uff0c<code>\\s<\/code>\u8868\u793a\u5339\u914d\u4efb\u610f\u7684\u7a7a\u767d\u5b57\u7b26\uff0c<code>*<\/code>\u5c31\u4ee3\u8868\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u4efb\u610f\u591a\u4e2a\uff0c\u8fd9\u4e00\u957f\u4e32\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5c31\u662f\u8fd9\u4e48\u591a\u5339\u914d\u89c4\u5219\u7684\u7ec4\u5408\u3002<\/p>\n\n\n\n<p>\u5199\u597d\u6b63\u5219\u8868\u8fbe\u5f0f\u540e\uff0c\u5c31\u53ef\u4ee5\u62ff\u5b83\u53bb\u4e00\u4e2a\u957f\u5b57\u7b26\u4e32\u91cc\u5339\u914d\u67e5\u627e\u4e86\u3002\u4e0d\u8bba\u8fd9\u4e2a\u5b57\u7b26\u4e32\u91cc\u9762\u6709\u4ec0\u4e48\uff0c\u53ea\u8981\u7b26\u5408\u6211\u4eec\u5199\u7684\u89c4\u5219\uff0c\u7edf\u7edf\u53ef\u4ee5\u627e\u51fa\u6765\u3002\u5bf9\u4e8e\u7f51\u9875\u6765\u8bf4\uff0c\u5982\u679c\u60f3\u627e\u51fa\u7f51\u9875\u6e90\u4ee3\u7801\u91cc\u6709\u591a\u5c11URL\uff0c\u7528\u5339\u914dURL\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u5339\u914d\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u4e0a\u9762\u6211\u4eec\u8bf4\u4e86\u51e0\u4e2a\u5339\u914d\u89c4\u5219\uff0c\u88683-2\u5217\u51fa\u4e86\u5e38\u7528\u7684\u5339\u914d\u89c4\u5219\u3002<\/p>\n\n\n\n<p>\u88683-2 \u5e38\u7528\u7684\u5339\u914d\u89c4\u5219<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><thead><tr><th>\n\u6a21\u5f0f\n<\/th><th>\n\u63cf\u8ff0\n<\/th><\/tr><\/thead><tbody><tr><td>\n<code>\\w<\/code>\n<\/td><td>\n\u5339\u914d\u5b57\u6bcd\u3001\u6570\u5b57\u53ca\u4e0b\u5212\u7ebf\n<\/td><\/tr><tr><td>\n<code>\\W<\/code>\n<\/td><td>\n\u5339\u914d\u4e0d\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u53ca\u4e0b\u5212\u7ebf\u7684\u5b57\u7b26\n<\/td><\/tr><tr><td>\n<code>\\s<\/code>\n<\/td><td>\n\u5339\u914d\u4efb\u610f\u7a7a\u767d\u5b57\u7b26\uff0c\u7b49\u4ef7\u4e8e<code>[\\t\\n\\r\\f]<\/code>\n<\/td><\/tr><tr><td>\n<code>\\S<\/code>\n<\/td><td>\n\u5339\u914d\u4efb\u610f\u975e\u7a7a\u5b57\u7b26\n<\/td><\/tr><tr><td>\n<code>\\d<\/code>\n<\/td><td>\n\u5339\u914d\u4efb\u610f\u6570\u5b57\uff0c\u7b49\u4ef7\u4e8e<code>[0-9]<\/code>\n<\/td><\/tr><tr><td>\n<code>\\D<\/code>\n<\/td><td>\n\u5339\u914d\u4efb\u610f\u975e\u6570\u5b57\u7684\u5b57\u7b26\n<\/td><\/tr><tr><td>\n<code>\\A<\/code>\n<\/td><td>\n\u5339\u914d\u5b57\u7b26\u4e32\u5f00\u5934\n<\/td><\/tr><tr><td>\n<code>\\Z<\/code>\n<\/td><td>\n\u5339\u914d\u5b57\u7b26\u4e32\u7ed3\u5c3e\uff0c\u5982\u679c\u5b58\u5728\u6362\u884c\uff0c\u53ea\u5339\u914d\u5230\u6362\u884c\u524d\u7684\u7ed3\u675f\u5b57\u7b26\u4e32\n<\/td><\/tr><tr><td>\n<code>\\z<\/code>\n<\/td><td>\n\u5339\u914d\u5b57\u7b26\u4e32\u7ed3\u5c3e\uff0c\u5982\u679c\u5b58\u5728\u6362\u884c\uff0c\u540c\u65f6\u8fd8\u4f1a\u5339\u914d\u6362\u884c\u7b26\n<\/td><\/tr><tr><td>\n<code>\\G<\/code>\n<\/td><td>\n\u5339\u914d\u6700\u540e\u5339\u914d\u5b8c\u6210\u7684\u4f4d\u7f6e\n<\/td><\/tr><tr><td>\n<code>\\n<\/code>\n<\/td><td>\n\u5339\u914d\u4e00\u4e2a\u6362\u884c\u7b26\n<\/td><\/tr><tr><td>\n<code>\\t<\/code>\n<\/td><td>\n\u5339\u914d\u4e00\u4e2a\u5236\u8868\u7b26\n<\/td><\/tr><tr><td>\n<code>^<\/code>\n<\/td><td>\n\u5339\u914d\u4e00\u884c\u5b57\u7b26\u4e32\u7684\u5f00\u5934\n<\/td><\/tr><tr><td>\n<code>$<\/code>\n<\/td><td>\n\u5339\u914d\u4e00\u884c\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\n<\/td><\/tr><tr><td>\n<code>.<\/code>\n<\/td><td>\n\u5339\u914d\u4efb\u610f\u5b57\u7b26\uff0c\u9664\u4e86\u6362\u884c\u7b26\uff0c\u5f53<code>re.DOTALL<\/code>\u6807\u8bb0\u88ab\u6307\u5b9a\u65f6\uff0c\u5219\u53ef\u4ee5\u5339\u914d\u5305\u62ec\u6362\u884c\u7b26\u7684\u4efb\u610f\u5b57\u7b26\n<\/td><\/tr><tr><td>\n<code>[...]<\/code>\n<\/td><td>\n\u7528\u6765\u8868\u793a\u4e00\u7ec4\u5b57\u7b26\uff0c\u5355\u72ec\u5217\u51fa\uff0c\u6bd4\u5982<code>[amk]<\/code>\u5339\u914d<code>a<\/code>\u3001<code>m<\/code>\u6216<code>k<\/code>\n<\/td><\/tr><tr><td>\n<code>[^...]<\/code>\n<\/td><td>\n\u4e0d\u5728<code>[]<\/code>\u4e2d\u7684\u5b57\u7b26\uff0c\u6bd4\u5982<code>[^abc]<\/code>\u5339\u914d\u9664\u4e86<code>a<\/code>\u3001<code>b<\/code>\u3001<code>c<\/code>\u4e4b\u5916\u7684\u5b57\u7b26\n<\/td><\/tr><tr><td>\n<code>*<\/code>\n<\/td><td>\n\u5339\u914d0\u4e2a\u6216\u591a\u4e2a\u8868\u8fbe\u5f0f\n<\/td><\/tr><tr><td>\n<code>+<\/code>\n<\/td><td>\n\u5339\u914d1\u4e2a\u6216\u591a\u4e2a\u8868\u8fbe\u5f0f\n<\/td><\/tr><tr><td>\n<code>?<\/code>\n<\/td><td>\n\u5339\u914d0\u4e2a\u62161\u4e2a\u524d\u9762\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u7684\u7247\u6bb5\uff0c\u975e\u8d2a\u5a6a\u65b9\u5f0f\n<\/td><\/tr><tr><td>\n<code>{n}<\/code>\n<\/td><td>\n\u7cbe\u786e\u5339\u914d<code>n<\/code>\u4e2a\u524d\u9762\u7684\u8868\u8fbe\u5f0f\n<\/td><\/tr><tr><td>\n<code>{n, m}<\/code>\n<\/td><td>\n\u5339\u914d<code>n<\/code>\u5230<code>m<\/code>\u6b21\u7531\u524d\u9762\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u7684\u7247\u6bb5\uff0c\u8d2a\u5a6a\u65b9\u5f0f\n<\/td><\/tr><tr><td>\n<code>a|b<\/code>\n<\/td><td>\n\u5339\u914d<code>a<\/code>\u6216<code>b<\/code>\n<\/td><\/tr><tr><td>\n<code>( )<\/code>\n<\/td><td>\n\u5339\u914d\u62ec\u53f7\u5185\u7684\u8868\u8fbe\u5f0f\uff0c\u4e5f\u8868\u793a\u4e00\u4e2a\u7ec4\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u770b\u5b8c\u4e86\u4e4b\u540e\uff0c\u53ef\u80fd\u6709\u70b9\u6655\u6655\u7684\u5427\uff0c\u4e0d\u8fc7\u4e0d\u7528\u62c5\u5fc3\uff0c\u540e\u9762\u6211\u4eec\u4f1a\u8be6\u7ec6\u8bb2\u89e3\u4e00\u4e9b\u5e38\u89c1\u89c4\u5219\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<p>\u5176\u5b9e\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0d\u662fPython\u72ec\u6709\u7684\uff0c\u5b83\u4e5f\u53ef\u4ee5\u7528\u5728\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u3002\u4f46\u662fPython\u7684re\u5e93\u63d0\u4f9b\u4e86\u6574\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b9e\u73b0\uff0c\u5229\u7528\u8fd9\u4e2a\u5e93\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5728Python\u4e2d\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u51e0\u4e4e\u90fd\u7528\u8fd9\u4e2a\u5e93\uff0c\u4e0b\u9762\u5c31\u6765\u4e86\u89e3\u5b83\u7684\u4e00\u4e9b\u5e38\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.&nbsp;<code>match()<\/code><\/h2>\n\n\n\n<p>\u8fd9\u91cc\u9996\u5148\u4ecb\u7ecd\u7b2c\u4e00\u4e2a\u5e38\u7528\u7684\u5339\u914d\u65b9\u6cd5\u2014\u2014<code>match()<\/code>\uff0c\u5411\u5b83\u4f20\u5165\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5c31\u53ef\u4ee5\u68c0\u6d4b\u8fd9\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5426\u5339\u914d\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p><code>match()<\/code>\u65b9\u6cd5\u4f1a\u5c1d\u8bd5\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u5339\u914d\uff0c\u5c31\u8fd4\u56de\u5339\u914d\u6210\u529f\u7684\u7ed3\u679c\uff1b\u5982\u679c\u4e0d\u5339\u914d\uff0c\u5c31\u8fd4\u56de<code>None<\/code>\u3002\u793a\u4f8b\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\timport re\n\ncontent = &#8216;Hello 123 4567 World_This is a Regex Demo&#8217;\nprint(len(content))\nresult = re.match(&#8216;^Hello\\s\\d\\d\\d\\s\\d{4}\\s\\w{10}&#8217;, content)\nprint(result)\nprint(result.group())\nprint(result.span())\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t12345678\n\t\t\t\t<\/td><td>import re&nbsp;content = &#8216;Hello 123 4567 World_This is a Regex Demo&#8217;print(len(content))result = re.match(&#8216;^Hello\\s\\d\\d\\d\\s\\d{4}\\s\\w{10}&#8217;, content)print(result)print(result.group())print(result.span())<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t41\n&lt;_sre.SRE_Match object; span=(0, 25), match=&#8217;Hello 123 4567 World_This&#8217;&gt;\nHello 123 4567 World_This\n(0, 25)\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1234\n\t\t\t\t<\/td><td>41&lt;_sre.SRE_Match object; span=(0, 25), match=&#8217;Hello 123 4567 World_This&#8217;&gt;Hello 123 4567 World_This(0, 25)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u9996\u5148\u58f0\u660e\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u82f1\u6587\u5b57\u6bcd\u3001\u7a7a\u767d\u5b57\u7b26\u3001\u6570\u5b57\u7b49\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5199\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t^Hello\\s\\d\\d\\d\\s\\d{4}\\s\\w{10}\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>^Hello\\s\\d\\d\\d\\s\\d{4}\\s\\w{10}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7528\u5b83\u6765\u5339\u914d\u8fd9\u4e2a\u957f\u5b57\u7b26\u4e32\u3002\u5f00\u5934\u7684<code>^<\/code>\u662f\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u5934\uff0c\u4e5f\u5c31\u662f\u4ee5<code>Hello<\/code>\u5f00\u5934\uff1b\u7136\u540e<code>\\s<\/code>\u5339\u914d\u7a7a\u767d\u5b57\u7b26\uff0c\u7528\u6765\u5339\u914d\u76ee\u6807\u5b57\u7b26\u4e32\u7684\u7a7a\u683c\uff1b<code>\\d<\/code>\u5339\u914d\u6570\u5b57\uff0c3\u4e2a<code>\\d<\/code>\u5339\u914d123\uff1b\u7136\u540e\u518d\u51991\u4e2a<code>\\s<\/code>\u5339\u914d\u7a7a\u683c\uff1b\u540e\u9762\u8fd8\u6709<code>4567<\/code>\uff0c\u6211\u4eec\u5176\u5b9e\u53ef\u4ee5\u4f9d\u7136\u75284\u4e2a<code>\\d<\/code>\u6765\u5339\u914d\uff0c\u4f46\u662f\u8fd9\u4e48\u5199\u6bd4\u8f83\u70e6\u7410\uff0c\u6240\u4ee5\u540e\u9762\u53ef\u4ee5\u8ddf<code>{4}<\/code>\u4ee5\u4ee3\u8868\u5339\u914d\u524d\u9762\u7684\u89c4\u52194\u6b21\uff0c\u4e5f\u5c31\u662f\u5339\u914d4\u4e2a\u6570\u5b57\uff1b\u7136\u540e\u540e\u9762\u518d\u7d27\u63a51\u4e2a\u7a7a\u767d\u5b57\u7b26\uff0c\u6700\u540e<code>\\w{10}<\/code>\u5339\u914d10\u4e2a\u5b57\u6bcd\u53ca\u4e0b\u5212\u7ebf\u3002\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u8fd9\u91cc\u5176\u5b9e\u5e76\u6ca1\u6709\u628a\u76ee\u6807\u5b57\u7b26\u4e32\u5339\u914d\u5b8c\uff0c\u4e0d\u8fc7\u8fd9\u6837\u4f9d\u7136\u53ef\u4ee5\u8fdb\u884c\u5339\u914d\uff0c\u53ea\u4e0d\u8fc7\u5339\u914d\u7ed3\u679c\u77ed\u4e00\u70b9\u800c\u5df2\u3002<\/p>\n\n\n\n<p>\u800c\u5728<code>match()<\/code>\u65b9\u6cd5\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20\u5165\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u5165\u4e86\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>\u6253\u5370\u8f93\u51fa\u7ed3\u679c\uff0c\u53ef\u4ee5\u770b\u5230\u7ed3\u679c\u662f<code>SRE_Match<\/code>\u5bf9\u8c61\uff0c\u8fd9\u8bc1\u660e\u6210\u529f\u5339\u914d\u3002\u8be5\u5bf9\u8c61\u6709\u4e24\u4e2a\u65b9\u6cd5\uff1a<code>group()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f93\u51fa\u5339\u914d\u5230\u7684\u5185\u5bb9\uff0c\u7ed3\u679c\u662f<code>Hello 123 4567 World_This<\/code>\uff0c\u8fd9\u6070\u597d\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u89c4\u5219\u6240\u5339\u914d\u7684\u5185\u5bb9\uff1b<code>span()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f93\u51fa\u5339\u914d\u7684\u8303\u56f4\uff0c\u7ed3\u679c\u662f<code>(0, 25)<\/code>\uff0c\u8fd9\u5c31\u662f\u5339\u914d\u5230\u7684\u7ed3\u679c\u5b57\u7b26\u4e32\u5728\u539f\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\u8303\u56f4\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u57fa\u672c\u4e86\u89e3\u4e86\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u4e00\u6bb5\u6587\u5b57\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5339\u914d\u76ee\u6807<\/h3>\n\n\n\n<p>\u521a\u624d\u6211\u4eec\u7528<code>match()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5f97\u5230\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u5185\u5bb9\uff0c\u4f46\u662f\u5982\u679c\u60f3\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u4e00\u90e8\u5206\u5185\u5bb9\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f\u5c31\u50cf\u6700\u524d\u9762\u7684\u5b9e\u4f8b\u4e00\u6837\uff0c\u4ece\u4e00\u6bb5\u6587\u672c\u4e2d\u63d0\u53d6\u51fa\u90ae\u4ef6\u6216\u7535\u8bdd\u53f7\u7801\u7b49\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528<code>()<\/code>\u62ec\u53f7\u5c06\u60f3\u63d0\u53d6\u7684\u5b50\u5b57\u7b26\u4e32\u62ec\u8d77\u6765\u3002<code>()<\/code>\u5b9e\u9645\u4e0a\u6807\u8bb0\u4e86\u4e00\u4e2a\u5b50\u8868\u8fbe\u5f0f\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\uff0c\u88ab\u6807\u8bb0\u7684\u6bcf\u4e2a\u5b50\u8868\u8fbe\u5f0f\u4f1a\u4f9d\u6b21\u5bf9\u5e94\u6bcf\u4e00\u4e2a\u5206\u7ec4\uff0c\u8c03\u7528<code>group()<\/code>\u65b9\u6cd5\u4f20\u5165\u5206\u7ec4\u7684\u7d22\u5f15\u5373\u53ef\u83b7\u53d6\u63d0\u53d6\u7684\u7ed3\u679c\u3002\u793a\u4f8b\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\timport re\n\ncontent = &#8216;Hello 1234567 World_This is a Regex Demo&#8217;\nresult = re.match(&#8216;^Hello\\s(\\d+)\\sWorld&#8217;, content)\nprint(result)\nprint(result.group())\nprint(result.group(1))\nprint(result.span())\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t12345678\n\t\t\t\t<\/td><td>import re&nbsp;content = &#8216;Hello 1234567 World_This is a Regex Demo&#8217;result = re.match(&#8216;^Hello\\s(\\d+)\\sWorld&#8217;, content)print(result)print(result.group())print(result.group(1))print(result.span())<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u60f3\u628a\u5b57\u7b26\u4e32\u4e2d\u7684<code>1234567<\/code>\u63d0\u53d6\u51fa\u6765\uff0c\u6b64\u65f6\u53ef\u4ee5\u5c06\u6570\u5b57\u90e8\u5206\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u7528<code>()<\/code>\u62ec\u8d77\u6765\uff0c\u7136\u540e\u8c03\u7528\u4e86<code>group(1)<\/code>\u83b7\u53d6\u5339\u914d\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t&lt;_sre.SRE_Match object; span=(0, 19), match=&#8217;Hello 1234567 World&#8217;&gt;\nHello 1234567 World\n1234567\n(0, 19)\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1234\n\t\t\t\t<\/td><td>&lt;_sre.SRE_Match object; span=(0, 19), match=&#8217;Hello 1234567 World&#8217;&gt;Hello 1234567 World1234567(0, 19)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u6210\u529f\u5f97\u5230\u4e86<code>1234567<\/code>\u3002\u8fd9\u91cc\u7528\u7684\u662f<code>group(1)<\/code>\uff0c\u5b83\u4e0e<code>group()<\/code>\u6709\u6240\u4e0d\u540c\uff0c\u540e\u8005\u4f1a\u8f93\u51fa\u5b8c\u6574\u7684\u5339\u914d\u7ed3\u679c\uff0c\u800c\u524d\u8005\u4f1a\u8f93\u51fa\u7b2c\u4e00\u4e2a\u88ab<code>()<\/code>\u5305\u56f4\u7684\u5339\u914d\u7ed3\u679c\u3002\u5047\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u540e\u9762\u8fd8\u6709<code>()<\/code>\u5305\u62ec\u7684\u5185\u5bb9\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f9d\u6b21\u7528<code>group(2)<\/code>\u3001<code>group(3)<\/code>\u7b49\u6765\u83b7\u53d6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u901a\u7528\u5339\u914d<\/h3>\n\n\n\n<p>\u521a\u624d\u6211\u4eec\u5199\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5176\u5b9e\u6bd4\u8f83\u590d\u6742\uff0c\u51fa\u73b0\u7a7a\u767d\u5b57\u7b26\u6211\u4eec\u5c31\u5199<code>\\s<\/code>\u5339\u914d\uff0c\u51fa\u73b0\u6570\u5b57\u6211\u4eec\u5c31\u7528<code>\\d<\/code>\u5339\u914d\uff0c\u8fd9\u6837\u7684\u5de5\u4f5c\u91cf\u975e\u5e38\u5927\u3002\u5176\u5b9e\u5b8c\u5168\u6ca1\u5fc5\u8981\u8fd9\u4e48\u505a\uff0c\u56e0\u4e3a\u8fd8\u6709\u4e00\u4e2a\u4e07\u80fd\u5339\u914d\u53ef\u4ee5\u7528\uff0c\u90a3\u5c31\u662f<code>.*<\/code>\uff08\u70b9\u661f\uff09\u3002\u5176\u4e2d<code>.<\/code>\uff08\u70b9\uff09\u53ef\u4ee5\u5339\u914d\u4efb\u610f\u5b57\u7b26\uff08\u9664\u6362\u884c\u7b26\uff09\uff0c<code>*<\/code>\uff08\u661f\uff09\u4ee3\u8868\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u65e0\u9650\u6b21\uff0c\u6240\u4ee5\u5b83\u4eec\u7ec4\u5408\u5728\u4e00\u8d77\u5c31\u53ef\u4ee5\u5339\u914d\u4efb\u610f\u5b57\u7b26\u4e86\u3002\u6709\u4e86\u5b83\uff0c\u6211\u4eec\u5c31\u4e0d\u7528\u6328\u4e2a\u5b57\u7b26\u5730\u5339\u914d\u4e86\u3002<\/p>\n\n\n\n<p>\u63a5\u7740\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u5199\u4e00\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\timport re\n\ncontent = &#8216;Hello 123 4567 World_This is a Regex Demo&#8217;\nresult = re.match(&#8216;^Hello.*Demo$&#8217;, content)\nprint(result)\nprint(result.group())\nprint(result.span())\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1234567\n\t\t\t\t<\/td><td>import re&nbsp;content = &#8216;Hello 123 4567 World_This is a Regex Demo&#8217;result = re.match(&#8216;^Hello.*Demo$&#8217;, content)print(result)print(result.group())print(result.span())<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u5c06\u4e2d\u95f4\u90e8\u5206\u76f4\u63a5\u7701\u7565\uff0c\u5168\u90e8\u7528<code>.*<\/code>\u6765\u4ee3\u66ff\uff0c\u6700\u540e\u52a0\u4e00\u4e2a\u7ed3\u5c3e\u5b57\u7b26\u4e32\u5c31\u597d\u4e86\u3002\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t&lt;_sre.SRE_Match object; span=(0, 41), match=&#8217;Hello 123 4567 World_This is a Regex Demo&#8217;&gt;\nHello 123 4567 World_This is a Regex Demo\n(0, 41)\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t123\n\t\t\t\t<\/td><td>&lt;_sre.SRE_Match object; span=(0, 41), match=&#8217;Hello 123 4567 World_This is a Regex Demo&#8217;&gt;Hello 123 4567 World_This is a Regex Demo(0, 41)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<code>group()<\/code>\u65b9\u6cd5\u8f93\u51fa\u4e86\u5339\u914d\u7684\u5168\u90e8\u5b57\u7b26\u4e32\uff0c\u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u5199\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5230\u4e86\u76ee\u6807\u5b57\u7b26\u4e32\u7684\u5168\u90e8\u5185\u5bb9\uff1b<code>span()<\/code>\u65b9\u6cd5\u8f93\u51fa<code>(0, 41)<\/code>\uff0c\u8fd9\u662f\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>.*<\/code>\u7b80\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e66\u5199\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8d2a\u5a6a\u4e0e\u975e\u8d2a\u5a6a<\/h3>\n\n\n\n<p>\u4f7f\u7528\u4e0a\u9762\u7684\u901a\u7528\u5339\u914d<code>.*<\/code>\u65f6\uff0c\u53ef\u80fd\u6709\u65f6\u5019\u5339\u914d\u5230\u7684\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\timport re\n\ncontent = &#8216;Hello 1234567 World_This is a Regex Demo&#8217;\nresult = re.match(&#8216;^He.*(\\d+).*Demo$&#8217;, content)\nprint(result)\nprint(result.group(1))\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t123456\n\t\t\t\t<\/td><td>import re&nbsp;content = &#8216;Hello 1234567 World_This is a Regex Demo&#8217;result = re.match(&#8216;^He.*(\\d+).*Demo$&#8217;, content)print(result)print(result.group(1))<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u4f9d\u7136\u60f3\u83b7\u53d6\u4e2d\u95f4\u7684\u6570\u5b57\uff0c\u6240\u4ee5\u4e2d\u95f4\u4f9d\u7136\u5199\u7684\u662f<code>(\\d+)<\/code>\u3002\u800c\u6570\u5b57\u4e24\u4fa7\u7531\u4e8e\u5185\u5bb9\u6bd4\u8f83\u6742\u4e71\uff0c\u6240\u4ee5\u60f3\u7701\u7565\u6765\u5199\uff0c\u90fd\u5199\u6210&nbsp;<code>.*<\/code>\u3002\u6700\u540e\uff0c\u7ec4\u6210<code>^He.*(\\d+).*Demo$<\/code>\uff0c\u770b\u6837\u5b50\u5e76\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\u3002\u6211\u4eec\u770b\u4e0b\u8fd0\u884c\u7ed3\u679c\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t&lt;_sre.SRE_Match object; span=(0, 40), match=&#8217;Hello 1234567 World_This is a Regex Demo&#8217;&gt;\n7\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t12\n\t\t\t\t<\/td><td>&lt;_sre.SRE_Match object; span=(0, 40), match=&#8217;Hello 1234567 World_This is a Regex Demo&#8217;&gt;7<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5947\u602a\u7684\u4e8b\u60c5\u53d1\u751f\u4e86\uff0c\u6211\u4eec\u53ea\u5f97\u5230\u4e867\u8fd9\u4e2a\u6570\u5b57\uff0c\u8fd9\u662f\u600e\u4e48\u56de\u4e8b\u5462\uff1f<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5c31\u6d89\u53ca\u4e00\u4e2a\u8d2a\u5a6a\u5339\u914d\u4e0e\u975e\u8d2a\u5a6a\u5339\u914d\u7684\u95ee\u9898\u4e86\u3002\u5728\u8d2a\u5a6a\u5339\u914d\u4e0b\uff0c<code>.*<\/code>\u4f1a\u5339\u914d\u5c3d\u53ef\u80fd\u591a\u7684\u5b57\u7b26\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d<code>.*<\/code>\u540e\u9762\u662f<code>\\d+<\/code>\uff0c\u4e5f\u5c31\u662f\u81f3\u5c11\u4e00\u4e2a\u6570\u5b57\uff0c\u5e76\u6ca1\u6709\u6307\u5b9a\u5177\u4f53\u591a\u5c11\u4e2a\u6570\u5b57\uff0c\u56e0\u6b64\uff0c<code>.*<\/code>\u5c31\u5c3d\u53ef\u80fd\u5339\u914d\u591a\u7684\u5b57\u7b26\uff0c\u8fd9\u91cc\u5c31\u628a<code>123456<\/code>\u5339\u914d\u4e86\uff0c\u7ed9<code>\\d+<\/code>\u7559\u4e0b\u4e00\u4e2a\u53ef\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u5b577\uff0c\u6700\u540e\u5f97\u5230\u7684\u5185\u5bb9\u5c31\u53ea\u6709\u6570\u5b577\u4e86\u3002<\/p>\n\n\n\n<p>\u4f46\u8fd9\u5f88\u660e\u663e\u4f1a\u7ed9\u6211\u4eec\u5e26\u6765\u5f88\u5927\u7684\u4e0d\u4fbf\u3002\u6709\u65f6\u5019\uff0c\u5339\u914d\u7ed3\u679c\u4f1a\u83ab\u540d\u5176\u5999\u5c11\u4e86\u4e00\u90e8\u5206\u5185\u5bb9\u3002\u5176\u5b9e\uff0c\u8fd9\u91cc\u53ea\u9700\u8981\u4f7f\u7528\u975e\u8d2a\u5a6a\u5339\u914d\u5c31\u597d\u4e86\u3002\u975e\u8d2a\u5a6a\u5339\u914d\u7684\u5199\u6cd5\u662f<code>.*?<\/code>\uff0c\u591a\u4e86\u4e00\u4e2a<code>?<\/code>\uff0c\u90a3\u4e48\u5b83\u53ef\u4ee5\u8fbe\u5230\u600e\u6837\u7684\u6548\u679c\uff1f\u6211\u4eec\u518d\u7528\u5b9e\u4f8b\u770b\u4e00\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\timport re\n\ncontent = &#8216;Hello 1234567 World_This is a Regex Demo&#8217;\nresult = re.match(&#8216;^He.*?(\\d+).*Demo$&#8217;, content)\nprint(result)\nprint(result.group(1))\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t123456\n\t\t\t\t<\/td><td>import re&nbsp;content = &#8216;Hello 1234567 World_This is a Regex Demo&#8217;result = re.match(&#8216;^He.*?(\\d+).*Demo$&#8217;, content)print(result)print(result.group(1))<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u53ea\u662f\u5c06\u7b2c\u4e00\u4e2a<code>.*<\/code>\u6539\u6210\u4e86<code>.*?<\/code>\uff0c\u8f6c\u53d8\u4e3a\u975e\u8d2a\u5a6a\u5339\u914d\u3002\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t&lt;_sre.SRE_Match object; span=(0, 40), match=&#8217;Hello 1234567 World_This is a Regex Demo&#8217;&gt;\n1234567\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t12\n\t\t\t\t<\/td><td>&lt;_sre.SRE_Match object; span=(0, 40), match=&#8217;Hello 1234567 World_This is a Regex Demo&#8217;&gt;1234567<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6b64\u65f6\u5c31\u53ef\u4ee5\u6210\u529f\u83b7\u53d6<code>1234567<\/code>\u4e86\u3002\u539f\u56e0\u53ef\u60f3\u800c\u77e5\uff0c\u8d2a\u5a6a\u5339\u914d\u662f\u5c3d\u53ef\u80fd\u5339\u914d\u591a\u7684\u5b57\u7b26\uff0c\u975e\u8d2a\u5a6a\u5339\u914d\u5c31\u662f\u5c3d\u53ef\u80fd\u5339\u914d\u5c11\u7684\u5b57\u7b26\u3002\u5f53<code>.*?<\/code>\u5339\u914d\u5230<code>Hello<\/code>\u540e\u9762\u7684\u7a7a\u767d\u5b57\u7b26\u65f6\uff0c\u518d\u5f80\u540e\u7684\u5b57\u7b26\u5c31\u662f\u6570\u5b57\u4e86\uff0c\u800c<code>\\d+<\/code>\u6070\u597d\u53ef\u4ee5\u5339\u914d\uff0c\u90a3\u4e48\u8fd9\u91cc<code>.*?<\/code>\u5c31\u4e0d\u518d\u8fdb\u884c\u5339\u914d\uff0c\u4ea4\u7ed9<code>\\d+<\/code>\u53bb\u5339\u914d\u540e\u9762\u7684\u6570\u5b57\u3002\u6240\u4ee5\u8fd9\u6837<code>.*?<\/code>\u5339\u914d\u4e86\u5c3d\u53ef\u80fd\u5c11\u7684\u5b57\u7b26\uff0c<code>\\d+<\/code>\u7684\u7ed3\u679c\u5c31\u662f<code>1234567<\/code>\u4e86\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u8bf4\uff0c\u5728\u505a\u5339\u914d\u7684\u65f6\u5019\uff0c\u5b57\u7b26\u4e32\u4e2d\u95f4\u5c3d\u91cf\u4f7f\u7528\u975e\u8d2a\u5a6a\u5339\u914d\uff0c\u4e5f\u5c31\u662f\u7528<code>.*?<\/code>\u6765\u4ee3\u66ff<code>.*<\/code>\uff0c\u4ee5\u514d\u51fa\u73b0\u5339\u914d\u7ed3\u679c\u7f3a\u5931\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u4f46\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\uff0c\u5982\u679c\u5339\u914d\u7684\u7ed3\u679c\u5728\u5b57\u7b26\u4e32\u7ed3\u5c3e\uff0c<code>.*?<\/code>\u5c31\u6709\u53ef\u80fd\u5339\u914d\u4e0d\u5230\u4efb\u4f55\u5185\u5bb9\u4e86\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5339\u914d\u5c3d\u53ef\u80fd\u5c11\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\timport re\n\ncontent = &#8216;http:\/\/weibo.com\/comment\/kEraCN&#8217;\nresult1 = re.match(&#8216;http.*?comment\/(.*?)&#8217;, content)\nresult2 = re.match(&#8216;http.*?comment\/(.*)&#8217;, content)\nprint(&#8216;result1&#8217;, result1.group(1))\nprint(&#8216;result2&#8217;, result2.group(1))\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1234567\n\t\t\t\t<\/td><td>import re&nbsp;content = &#8216;http:\/\/weibo.com\/comment\/kEraCN&#8217;result1 = re.match(&#8216;http.*?comment\/(.*?)&#8217;, content)result2 = re.match(&#8216;http.*?comment\/(.*)&#8217;, content)print(&#8216;result1&#8217;, result1.group(1))print(&#8216;result2&#8217;, result2.group(1))<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\tresult1 \nresult2 kEraCN\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t12\n\t\t\t\t<\/td><td>result1 result2 kEraCN<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c<code>.*?<\/code>\u6ca1\u6709\u5339\u914d\u5230\u4efb\u4f55\u7ed3\u679c\uff0c\u800c<code>.*<\/code>\u5219\u5c3d\u91cf\u5339\u914d\u591a\u7684\u5185\u5bb9\uff0c\u6210\u529f\u5f97\u5230\u4e86\u5339\u914d\u7ed3\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4fee\u9970\u7b26<\/h3>\n\n\n\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5305\u542b\u4e00\u4e9b\u53ef\u9009\u6807\u5fd7\u4fee\u9970\u7b26\u6765\u63a7\u5236\u5339\u914d\u7684\u6a21\u5f0f\u3002\u4fee\u9970\u7b26\u88ab\u6307\u5b9a\u4e3a\u4e00\u4e2a\u53ef\u9009\u7684\u6807\u5fd7\u3002\u6211\u4eec\u7528\u5b9e\u4f8b\u6765\u770b\u4e00\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\timport re\n\ncontent = &#8221;&#8217;Hello 1234567 World_This\nis a Regex Demo\n&#8221;&#8217;\nresult = re.match(&#8216;^He.*?(\\d+).*?Demo$&#8217;, content)\nprint(result.group(1))\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1234567\n\t\t\t\t<\/td><td>import re&nbsp;content = &#8221;&#8217;Hello 1234567 World_Thisis a Regex Demo&#8221;&#8217;result = re.match(&#8216;^He.*?(\\d+).*?Demo$&#8217;, content)print(result.group(1))<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u548c\u4e0a\u9762\u7684\u4f8b\u5b50\u76f8\u4eff\uff0c\u6211\u4eec\u5728\u5b57\u7b26\u4e32\u4e2d\u52a0\u4e86\u6362\u884c\u7b26\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd8\u662f\u4e00\u6837\u7684\uff0c\u7528\u6765\u5339\u914d\u5176\u4e2d\u7684\u6570\u5b57\u3002\u770b\u4e00\u4e0b\u8fd0\u884c\u7ed3\u679c\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\tAttributeError Traceback (most recent call last)\n&lt;ipython-input-18-c7d232b39645&gt; in &lt;module&gt;()\n      5 &#8221;&#8217;\n      6 result = re.match(&#8216;^He.*?(\\d+).*?Demo$&#8217;, content)\n&#8212;-&gt; 7 print(result.group(1))\n\nAttributeError: &#8216;NoneType&#8217; object has no attribute &#8216;group&#8217;\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1234567\n\t\t\t\t<\/td><td>AttributeError Traceback (most recent call last)&lt;ipython-input-18-c7d232b39645&gt; in &lt;module&gt;()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5 &#8221;&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6 result = re.match(&#8216;^He.*?(\\d+).*?Demo$&#8217;, content)&#8212;-&gt; 7 print(result.group(1))&nbsp;AttributeError: &#8216;NoneType&#8217; object has no attribute &#8216;group&#8217;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd0\u884c\u76f4\u63a5\u62a5\u9519\uff0c\u4e5f\u5c31\u662f\u8bf4\u6b63\u5219\u8868\u8fbe\u5f0f\u6ca1\u6709\u5339\u914d\u5230\u8fd9\u4e2a\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u7ed3\u679c\u4e3a<code>None<\/code>\uff0c\u800c\u6211\u4eec\u53c8\u8c03\u7528\u4e86<code>group()<\/code>\u65b9\u6cd5\u5bfc\u81f4<code>AttributeError<\/code>\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\uff0c\u4e3a\u4ec0\u4e48\u52a0\u4e86\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u5c31\u5339\u914d\u4e0d\u5230\u4e86\u5462\uff1f\u8fd9\u662f\u56e0\u4e3a<code>\\.<\/code>\u5339\u914d\u7684\u662f\u9664\u6362\u884c\u7b26\u4e4b\u5916\u7684\u4efb\u610f\u5b57\u7b26\uff0c\u5f53\u9047\u5230\u6362\u884c\u7b26\u65f6\uff0c<code>.*?<\/code>\u5c31\u4e0d\u80fd\u5339\u914d\u4e86\uff0c\u6240\u4ee5\u5bfc\u81f4\u5339\u914d\u5931\u8d25\u3002\u8fd9\u91cc\u53ea\u9700\u52a0\u4e00\u4e2a\u4fee\u9970\u7b26<code>re.S<\/code>\uff0c\u5373\u53ef\u4fee\u6b63\u8fd9\u4e2a\u9519\u8bef\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\tresult = re.match(&#8216;^He.*?(\\d+).*?Demo$&#8217;, content, re.S)\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>result = re.match(&#8216;^He.*?(\\d+).*?Demo$&#8217;, content, re.S)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u4e2a\u4fee\u9970\u7b26\u7684\u4f5c\u7528\u662f\u4f7f<code>.<\/code>\u5339\u914d\u5305\u62ec\u6362\u884c\u7b26\u5728\u5185\u7684\u6240\u6709\u5b57\u7b26\u3002\u6b64\u65f6\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t1234567\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>1234567<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u4e2a<code>re.S<\/code>\u5728\u7f51\u9875\u5339\u914d\u4e2d\u7ecf\u5e38\u7528\u5230\u3002\u56e0\u4e3aHTML\u8282\u70b9\u7ecf\u5e38\u4f1a\u6709\u6362\u884c\uff0c\u52a0\u4e0a\u5b83\uff0c\u5c31\u53ef\u4ee5\u5339\u914d\u8282\u70b9\u4e0e\u8282\u70b9\u4e4b\u95f4\u7684\u6362\u884c\u4e86\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u4fee\u9970\u7b26\uff0c\u5728\u5fc5\u8981\u7684\u60c5\u51b5\u4e0b\u4e5f\u53ef\u4ee5\u4f7f\u7528\uff0c\u5982\u88683-3\u6240\u793a\u3002<\/p>\n\n\n\n<p>\u88683-3 \u4fee\u9970\u7b26<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><thead><tr><th>\n\u4fee\u9970\u7b26\n<\/th><th>\n\u63cf\u8ff0\n<\/th><\/tr><\/thead><tbody><tr><td>\n<code>re.I<\/code>\n<\/td><td>\n\u4f7f\u5339\u914d\u5bf9\u5927\u5c0f\u5199\u4e0d\u654f\u611f\n<\/td><\/tr><tr><td>\n<code>re.L<\/code>\n<\/td><td>\n\u505a\u672c\u5730\u5316\u8bc6\u522b\uff08locale-aware\uff09\u5339\u914d\n<\/td><\/tr><tr><td>\n<code>re.M<\/code>\n<\/td><td>\n\u591a\u884c\u5339\u914d\uff0c\u5f71\u54cd<code>^<\/code>\u548c<code>$<\/code>\n<\/td><\/tr><tr><td>\n<code>re.S<\/code>\n<\/td><td>\n\u4f7f<code>.<\/code>\u5339\u914d\u5305\u62ec\u6362\u884c\u5728\u5185\u7684\u6240\u6709\u5b57\u7b26\n<\/td><\/tr><tr><td>\n<code>re.U<\/code>\n<\/td><td>\n\u6839\u636eUnicode\u5b57\u7b26\u96c6\u89e3\u6790\u5b57\u7b26\u3002\u8fd9\u4e2a\u6807\u5fd7\u5f71\u54cd<code>\\w<\/code>\u3001<code>\\W<\/code>\u3001&nbsp;<code>\\b<\/code>\u548c<code>\\B<\/code>\n<\/td><\/tr><tr><td>\n<code>re.X<\/code>\n<\/td><td>\n\u8be5\u6807\u5fd7\u901a\u8fc7\u7ed9\u4e88\u4f60\u66f4\u7075\u6d3b\u7684\u683c\u5f0f\u4ee5\u4fbf\u4f60\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u5199\u5f97\u66f4\u6613\u4e8e\u7406\u89e3\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5728\u7f51\u9875\u5339\u914d\u4e2d\uff0c\u8f83\u4e3a\u5e38\u7528\u7684\u6709<code>re.S<\/code>\u548c<code>re.I<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f6c\u4e49\u5339\u914d<\/h3>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u4e86\u8bb8\u591a\u5339\u914d\u6a21\u5f0f\uff0c\u5982<code>.<\/code>\u5339\u914d\u9664\u6362\u884c\u7b26\u4ee5\u5916\u7684\u4efb\u610f\u5b57\u7b26\uff0c\u4f46\u662f\u5982\u679c\u76ee\u6807\u5b57\u7b26\u4e32\u91cc\u9762\u5c31\u5305\u542b<code>.<\/code>\uff0c\u90a3\u8be5\u600e\u4e48\u529e\u5462\uff1f<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5c31\u9700\u8981\u7528\u5230\u8f6c\u4e49\u5339\u914d\u4e86\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\timport re\n\ncontent = &#8216;(\u767e\u5ea6)www.baidu.com&#8217;\nresult = re.match(&#8216;\\(\u767e\u5ea6\\)www\\.baidu\\.com&#8217;, content)\nprint(result)\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t12345\n\t\t\t\t<\/td><td>import re&nbsp;content = &#8216;(\u767e\u5ea6)www.baidu.com&#8217;result = re.match(&#8216;\\(\u767e\u5ea6\\)www\\.baidu\\.com&#8217;, content)print(result)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5f53\u9047\u5230\u7528\u4e8e\u6b63\u5219\u5339\u914d\u6a21\u5f0f\u7684\u7279\u6b8a\u5b57\u7b26\u65f6\uff0c\u5728\u524d\u9762\u52a0\u53cd\u659c\u7ebf\u8f6c\u4e49\u4e00\u4e0b\u5373\u53ef\u3002\u4f8b\u5982<code>.<\/code>\u5c31\u53ef\u4ee5\u7528<code>\\.<\/code>\u6765\u5339\u914d\uff0c\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t&lt;_sre.SRE_Match object; span=(0, 17), match='(\u767e\u5ea6)www.baidu.com&#8217;&gt;\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>&lt;_sre.SRE_Match object; span=(0, 17), match='(\u767e\u5ea6)www.baidu.com&#8217;&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u91cc\u6210\u529f\u5339\u914d\u5230\u4e86\u539f\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e9b\u662f\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u5e38\u7528\u7684\u51e0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u719f\u7ec3\u638c\u63e1\u5b83\u4eec\u5bf9\u540e\u9762\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.&nbsp;<code>search()<\/code><\/h2>\n\n\n\n<p>\u524d\u9762\u63d0\u5230\u8fc7\uff0c<code>match()<\/code>\u65b9\u6cd5\u662f\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u5f00\u59cb\u5339\u914d\u7684\uff0c\u4e00\u65e6\u5f00\u5934\u4e0d\u5339\u914d\uff0c\u90a3\u4e48\u6574\u4e2a\u5339\u914d\u5c31\u5931\u8d25\u4e86\u3002\u6211\u4eec\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\timport re\n\ncontent = &#8216;Extra stings Hello 1234567 World_This is a Regex Demo Extra stings&#8217;\nresult = re.match(&#8216;Hello.*?(\\d+).*?Demo&#8217;, content)\nprint(result)\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t12345\n\t\t\t\t<\/td><td>import re&nbsp;content = &#8216;Extra stings Hello 1234567 World_This is a Regex Demo Extra stings&#8217;result = re.match(&#8216;Hello.*?(\\d+).*?Demo&#8217;, content)print(result)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u7684\u5b57\u7b26\u4e32\u4ee5<code>Extra<\/code>\u5f00\u5934\uff0c\u4f46\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u4ee5<code>Hello<\/code>\u5f00\u5934\uff0c\u6574\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\uff0c\u4f46\u662f\u8fd9\u6837\u5339\u914d\u662f\u5931\u8d25\u7684\u3002\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\tNone\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>None<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u56e0\u4e3a<code>match()<\/code>\u65b9\u6cd5\u5728\u4f7f\u7528\u65f6\u9700\u8981\u8003\u8651\u5230\u5f00\u5934\u7684\u5185\u5bb9\uff0c\u8fd9\u5728\u505a\u5339\u914d\u65f6\u5e76\u4e0d\u65b9\u4fbf\u3002\u5b83\u66f4\u9002\u5408\u7528\u6765\u68c0\u6d4b\u67d0\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408\u67d0\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u89c4\u5219\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5c31\u6709\u53e6\u5916\u4e00\u4e2a\u65b9\u6cd5<code>search()<\/code>\uff0c\u5b83\u5728\u5339\u914d\u65f6\u4f1a\u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6210\u529f\u5339\u914d\u7684\u7ed3\u679c\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\uff0c\u5728\u5339\u914d\u65f6\uff0c<code>search()<\/code>\u65b9\u6cd5\u4f1a\u4f9d\u6b21\u626b\u63cf\u5b57\u7b26\u4e32\uff0c\u76f4\u5230\u627e\u5230\u7b2c\u4e00\u4e2a\u7b26\u5408\u89c4\u5219\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8fd4\u56de\u5339\u914d\u5185\u5bb9\uff0c\u5982\u679c\u641c\u7d22\u5b8c\u4e86\u8fd8\u6ca1\u6709\u627e\u5230\uff0c\u5c31\u8fd4\u56de<code>None<\/code>\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u628a\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684<code>match()<\/code>\u65b9\u6cd5\u4fee\u6539\u6210<code>search()<\/code>\uff0c\u518d\u770b\u4e0b\u8fd0\u884c\u7ed3\u679c\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t&lt;_sre.SRE_Match object; span=(13, 53), match=&#8217;Hello 1234567 World_This is a Regex Demo&#8217;&gt;\n1234567\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t12\n\t\t\t\t<\/td><td>&lt;_sre.SRE_Match object; span=(13, 53), match=&#8217;Hello 1234567 World_This is a Regex Demo&#8217;&gt;1234567<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u65f6\u5c31\u5f97\u5230\u4e86\u5339\u914d\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u4e3a\u4e86\u5339\u914d\u65b9\u4fbf\uff0c\u6211\u4eec\u53ef\u4ee5\u5c3d\u91cf\u4f7f\u7528<code>search()<\/code>\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u518d\u7528\u51e0\u4e2a\u5b9e\u4f8b\u6765\u770b\u770b<code>search()<\/code>\u65b9\u6cd5\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u8fd9\u91cc\u6709\u4e00\u6bb5\u5f85\u5339\u914d\u7684HTML\u6587\u672c\uff0c\u63a5\u4e0b\u6765\u5199\u51e0\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u4f8b\u6765\u5b9e\u73b0\u76f8\u5e94\u4fe1\u606f\u7684\u63d0\u53d6\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\thtml = &#8221;'&lt;div id=&#8221;songs-list&#8221;&gt;\n    &lt;h2 class=&#8221;title&#8221;&gt;\u7ecf\u5178\u8001\u6b4c&lt;\/h2&gt;\n    &lt;p class=&#8221;introduction&#8221;&gt;\n        \u7ecf\u5178\u8001\u6b4c\u5217\u8868\n    &lt;\/p&gt;\n    &lt;ul id=&#8221;list&#8221; class=&#8221;list-group&#8221;&gt;\n        &lt;li data-view=&#8221;2&#8243;&gt;\u4e00\u8def\u4e0a\u6709\u4f60&lt;\/li&gt;\n        &lt;li data-view=&#8221;7&#8243;&gt;\n            &lt;a href=&#8221;\/2.mp3&#8243; singer=&#8221;\u4efb\u8d24\u9f50&#8221;&gt;\u6ca7\u6d77\u4e00\u58f0\u7b11&lt;\/a&gt;\n        &lt;\/li&gt;\n        &lt;li data-view=&#8221;4&#8243; class=&#8221;active&#8221;&gt;\n            &lt;a href=&#8221;\/3.mp3&#8243; singer=&#8221;\u9f50\u79e6&#8221;&gt;\u5f80\u4e8b\u968f\u98ce&lt;\/a&gt;\n        &lt;\/li&gt;\n        &lt;li data-view=&#8221;6&#8243;&gt;&lt;a href=&#8221;\/4.mp3&#8243; singer=&#8221;beyond&#8221;&gt;\u5149\u8f89\u5c81\u6708&lt;\/a&gt;&lt;\/li&gt;\n        &lt;li data-view=&#8221;5&#8243;&gt;&lt;a href=&#8221;\/5.mp3&#8243; singer=&#8221;\u9648\u6167\u7433&#8221;&gt;\u8bb0\u4e8b\u672c&lt;\/a&gt;&lt;\/li&gt;\n        &lt;li data-view=&#8221;5&#8243;&gt;\n            &lt;a href=&#8221;\/6.mp3&#8243; singer=&#8221;\u9093\u4e3d\u541b&#8221;&gt;&lt;i class=&#8221;fa fa-user&#8221;&gt;&lt;\/i&gt;\u4f46\u613f\u4eba\u957f\u4e45&lt;\/a&gt;\n        &lt;\/li&gt;\n    &lt;\/ul&gt;\n&lt;\/div&gt;&#8221;&#8217;\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1234567891011121314151617181920\n\t\t\t\t<\/td><td>html = &#8221;'&lt;div id=&#8221;songs-list&#8221;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2 class=&#8221;title&#8221;&gt;\u7ecf\u5178\u8001\u6b4c&lt;\/h2&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p class=&#8221;introduction&#8221;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u7ecf\u5178\u8001\u6b4c\u5217\u8868&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/p&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;ul id=&#8221;list&#8221; class=&#8221;list-group&#8221;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;2&#8243;&gt;\u4e00\u8def\u4e0a\u6709\u4f60&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;7&#8243;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;a href=&#8221;\/2.mp3&#8243; singer=&#8221;\u4efb\u8d24\u9f50&#8221;&gt;\u6ca7\u6d77\u4e00\u58f0\u7b11&lt;\/a&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;4&#8243; class=&#8221;active&#8221;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;a href=&#8221;\/3.mp3&#8243; singer=&#8221;\u9f50\u79e6&#8221;&gt;\u5f80\u4e8b\u968f\u98ce&lt;\/a&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;6&#8243;&gt;&lt;a href=&#8221;\/4.mp3&#8243; singer=&#8221;beyond&#8221;&gt;\u5149\u8f89\u5c81\u6708&lt;\/a&gt;&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;5&#8243;&gt;&lt;a href=&#8221;\/5.mp3&#8243; singer=&#8221;\u9648\u6167\u7433&#8221;&gt;\u8bb0\u4e8b\u672c&lt;\/a&gt;&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;5&#8243;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;a href=&#8221;\/6.mp3&#8243; singer=&#8221;\u9093\u4e3d\u541b&#8221;&gt;&lt;i class=&#8221;fa fa-user&#8221;&gt;&lt;\/i&gt;\u4f46\u613f\u4eba\u957f\u4e45&lt;\/a&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/ul&gt;&lt;\/div&gt;&#8221;&#8217;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c<code>ul<\/code>\u8282\u70b9\u91cc\u6709\u8bb8\u591a<code>li<\/code>\u8282\u70b9\uff0c\u5176\u4e2d<code>li<\/code>\u8282\u70b9\u4e2d\u6709\u7684\u5305\u542b<code>a<\/code>\u8282\u70b9\uff0c\u6709\u7684\u4e0d\u5305\u542b<code>a<\/code>\u8282\u70b9\uff0c<code>a<\/code>\u8282\u70b9\u8fd8\u6709\u4e00\u4e9b\u76f8\u5e94\u7684\u5c5e\u6027\u2014\u2014\u8d85\u94fe\u63a5\u548c\u6b4c\u624b\u540d\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u5c1d\u8bd5\u63d0\u53d6<code>class<\/code>\u4e3a<code>active<\/code>\u7684<code>li<\/code>\u8282\u70b9\u5185\u90e8\u7684\u8d85\u94fe\u63a5\u5305\u542b\u7684\u6b4c\u624b\u540d\u548c\u6b4c\u540d\uff0c\u6b64\u65f6\u9700\u8981\u63d0\u53d6\u7b2c\u4e09\u4e2a<code>li<\/code>\u8282\u70b9\u4e0b<code>a<\/code>\u8282\u70b9\u7684<code>singer<\/code>\u5c5e\u6027\u548c\u6587\u672c\u3002<\/p>\n\n\n\n<p>\u6b64\u65f6\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u4ee5<code>li<\/code>\u5f00\u5934\uff0c\u7136\u540e\u5bfb\u627e\u4e00\u4e2a\u6807\u5fd7\u7b26<code>active<\/code>\uff0c\u4e2d\u95f4\u7684\u90e8\u5206\u53ef\u4ee5\u7528<code>.*?<\/code>\u6765\u5339\u914d\u3002\u63a5\u4e0b\u6765\uff0c\u8981\u63d0\u53d6<code>singer<\/code>\u8fd9\u4e2a\u5c5e\u6027\u503c\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u5199\u5165<code>singer=\"(.*?)\"<\/code>\uff0c\u8fd9\u91cc\u9700\u8981\u63d0\u53d6\u7684\u90e8\u5206\u7528\u5c0f\u62ec\u53f7\u62ec\u8d77\u6765\uff0c\u4ee5\u4fbf\u7528<code>group()<\/code>\u65b9\u6cd5\u63d0\u53d6\u51fa\u6765\uff0c\u5b83\u7684\u4e24\u4fa7\u8fb9\u754c\u662f\u53cc\u5f15\u53f7\u3002\u7136\u540e\u8fd8\u9700\u8981\u5339\u914d<code>a<\/code>\u8282\u70b9\u7684\u6587\u672c\uff0c\u5176\u4e2d\u5b83\u7684\u5de6\u8fb9\u754c\u662f<code>&gt;<\/code>\uff0c\u53f3\u8fb9\u754c\u662f<code>&lt;\/a&gt;<\/code>\u3002\u7136\u540e\u76ee\u6807\u5185\u5bb9\u4f9d\u7136\u7528<code>(.*?)<\/code>\u6765\u5339\u914d\uff0c\u6240\u4ee5\u6700\u540e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5c31\u53d8\u6210\u4e86\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t&lt;li.*?active.*?singer=&#8221;(.*?)&#8221;&gt;(.*?)&lt;\/a&gt;\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>&lt;li.*?active.*?singer=&#8221;(.*?)&#8221;&gt;(.*?)&lt;\/a&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7136\u540e\u518d\u8c03\u7528<code>search()<\/code>\u65b9\u6cd5\uff0c\u5b83\u4f1a\u641c\u7d22\u6574\u4e2aHTML\u6587\u672c\uff0c\u627e\u5230\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b2c\u4e00\u4e2a\u5185\u5bb9\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u7531\u4e8e\u4ee3\u7801\u6709\u6362\u884c\uff0c\u6240\u4ee5\u8fd9\u91cc\u7b2c\u4e09\u4e2a\u53c2\u6570\u9700\u8981\u4f20\u5165<code>re.S<\/code>\u3002\u6574\u4e2a\u5339\u914d\u4ee3\u7801\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\tresult = re.search(&#8216;&lt;li.*?active.*?singer=&#8221;(.*?)&#8221;&gt;(.*?)&lt;\/a&gt;&#8217;, html, re.S)\nif result:\n    print(result.group(1), result.group(2))\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t123\n\t\t\t\t<\/td><td>result = re.search(&#8216;&lt;li.*?active.*?singer=&#8221;(.*?)&#8221;&gt;(.*?)&lt;\/a&gt;&#8217;, html, re.S)if result:&nbsp;&nbsp;&nbsp;&nbsp;print(result.group(1), result.group(2))<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7531\u4e8e\u9700\u8981\u83b7\u53d6\u7684\u6b4c\u624b\u548c\u6b4c\u540d\u90fd\u5df2\u7ecf\u7528\u5c0f\u62ec\u53f7\u5305\u56f4\uff0c\u6240\u4ee5\u53ef\u4ee5\u7528<code>group()<\/code>\u65b9\u6cd5\u83b7\u53d6\u3002<\/p>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\u9f50\u79e6 \u5f80\u4e8b\u968f\u98ce\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>\u9f50\u79e6 \u5f80\u4e8b\u968f\u98ce<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u6b63\u662f<code>class<\/code>\u4e3a<code>active<\/code>\u7684<code>li<\/code>\u8282\u70b9\u5185\u90e8\u7684\u8d85\u94fe\u63a5\u5305\u542b\u7684\u6b4c\u624b\u540d\u548c\u6b4c\u540d\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0d\u52a0<code>active<\/code>\uff08\u4e5f\u5c31\u662f\u5339\u914d\u4e0d\u5e26<code>class<\/code>\u4e3a<code>active<\/code>\u7684\u8282\u70b9\u5185\u5bb9\uff09\uff0c\u90a3\u4f1a\u600e\u6837\u5462\uff1f\u6211\u4eec\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684<code>active<\/code>\u53bb\u6389\uff0c\u4ee3\u7801\u6539\u5199\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\tresult = re.search(&#8216;&lt;li.*?singer=&#8221;(.*?)&#8221;&gt;(.*?)&lt;\/a&gt;&#8217;, html, re.S)\nif result:\n    print(result.group(1), result.group(2))\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t123\n\t\t\t\t<\/td><td>result = re.search(&#8216;&lt;li.*?singer=&#8221;(.*?)&#8221;&gt;(.*?)&lt;\/a&gt;&#8217;, html, re.S)if result:&nbsp;&nbsp;&nbsp;&nbsp;print(result.group(1), result.group(2))<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7531\u4e8esearch()\u65b9\u6cd5\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u5339\u914d\u76ee\u6807\uff0c\u8fd9\u91cc\u7ed3\u679c\u5c31\u53d8\u4e86\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\u4efb\u8d24\u9f50 \u6ca7\u6d77\u4e00\u58f0\u7b11\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>\u4efb\u8d24\u9f50 \u6ca7\u6d77\u4e00\u58f0\u7b11<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u628a<code>active<\/code>\u6807\u7b7e\u53bb\u6389\u540e\uff0c\u4ece\u5b57\u7b26\u4e32\u5f00\u5934\u5f00\u59cb\u641c\u7d22\uff0c\u6b64\u65f6\u7b26\u5408\u6761\u4ef6\u7684\u8282\u70b9\u5c31\u53d8\u6210\u4e86\u7b2c\u4e8c\u4e2a<code>li<\/code>\u8282\u70b9\uff0c\u540e\u9762\u7684\u5c31\u4e0d\u518d\u5339\u914d\uff0c\u6240\u4ee5\u8fd0\u884c\u7ed3\u679c\u5c31\u53d8\u6210\u7b2c\u4e8c\u4e2a<code>li<\/code>\u8282\u70b9\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0c\u5728\u4e0a\u9762\u7684\u4e24\u6b21\u5339\u914d\u4e2d\uff0c<code>search()<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u90fd\u52a0\u4e86<code>re.S<\/code>\uff0c\u8fd9\u4f7f\u5f97<code>.*?<\/code>\u53ef\u4ee5\u5339\u914d\u6362\u884c\uff0c\u6240\u4ee5\u542b\u6709\u6362\u884c\u7684<code>li<\/code>\u8282\u70b9\u88ab\u5339\u914d\u5230\u4e86\u3002\u5982\u679c\u6211\u4eec\u5c06\u5176\u53bb\u6389\uff0c\u7ed3\u679c\u4f1a\u662f\u4ec0\u4e48\uff1f\u4ee3\u7801\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\tresult = re.search(&#8216;&lt;li.*?singer=&#8221;(.*?)&#8221;&gt;(.*?)&lt;\/a&gt;&#8217;, html)\nif result:\n    print(result.group(1), result.group(2))\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t123\n\t\t\t\t<\/td><td>result = re.search(&#8216;&lt;li.*?singer=&#8221;(.*?)&#8221;&gt;(.*?)&lt;\/a&gt;&#8217;, html)if result:&nbsp;&nbsp;&nbsp;&nbsp;print(result.group(1), result.group(2))<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\tbeyond \u5149\u8f89\u5c81\u6708\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>beyond \u5149\u8f89\u5c81\u6708<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u7ed3\u679c\u53d8\u6210\u4e86\u7b2c\u56db\u4e2a<code>li<\/code>\u8282\u70b9\u7684\u5185\u5bb9\u3002\u8fd9\u662f\u56e0\u4e3a\u7b2c\u4e8c\u4e2a\u548c\u7b2c\u4e09\u4e2a<code>li<\/code>\u8282\u70b9\u90fd\u5305\u542b\u4e86\u6362\u884c\u7b26\uff0c\u53bb\u6389<code>re.S<\/code>\u4e4b\u540e\uff0c<code>.*?<\/code>\u5df2\u7ecf\u4e0d\u80fd\u5339\u914d\u6362\u884c\u7b26\uff0c\u6240\u4ee5\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0d\u4f1a\u5339\u914d\u5230\u7b2c\u4e8c\u4e2a\u548c\u7b2c\u4e09\u4e2a<code>li<\/code>\u8282\u70b9\uff0c\u800c\u7b2c\u56db\u4e2a<code>li<\/code>\u8282\u70b9\u4e2d\u4e0d\u5305\u542b\u6362\u884c\u7b26\uff0c\u6240\u4ee5\u6210\u529f\u5339\u914d\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u7edd\u5927\u90e8\u5206\u7684HTML\u6587\u672c\u90fd\u5305\u542b\u4e86\u6362\u884c\u7b26\uff0c\u6240\u4ee5\u5c3d\u91cf\u90fd\u9700\u8981\u52a0\u4e0a<code>re.S<\/code>\u4fee\u9970\u7b26\uff0c\u4ee5\u514d\u51fa\u73b0\u5339\u914d\u4e0d\u5230\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.&nbsp;<code>findall()<\/code><\/h2>\n\n\n\n<p>\u524d\u9762\u6211\u4eec\u4ecb\u7ecd\u4e86<code>search()<\/code>\u65b9\u6cd5\u7684\u7528\u6cd5\uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b2c\u4e00\u4e2a\u5185\u5bb9\uff0c\u4f46\u662f\u5982\u679c\u60f3\u8981\u83b7\u53d6\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6240\u6709\u5185\u5bb9\uff0c\u90a3\u8be5\u600e\u4e48\u529e\u5462\uff1f\u8fd9\u65f6\u5c31\u8981\u501f\u52a9<code>findall()<\/code>\u65b9\u6cd5\u4e86\u3002\u8be5\u65b9\u6cd5\u4f1a\u641c\u7d22\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8fd4\u56de\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6240\u6709\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u8fd8\u662f\u4e0a\u9762\u7684HTML\u6587\u672c\uff0c\u5982\u679c\u60f3\u83b7\u53d6\u6240\u6709<code>a<\/code>\u8282\u70b9\u7684\u8d85\u94fe\u63a5\u3001\u6b4c\u624b\u548c\u6b4c\u540d\uff0c\u5c31\u53ef\u4ee5\u5c06<code>search()<\/code>\u65b9\u6cd5\u6362\u6210<code>findall()<\/code>\u65b9\u6cd5\u3002\u5982\u679c\u6709\u8fd4\u56de\u7ed3\u679c\u7684\u8bdd\uff0c\u5c31\u662f\u5217\u8868\u7c7b\u578b\uff0c\u6240\u4ee5\u9700\u8981\u904d\u5386\u4e00\u4e0b\u6765\u4f9d\u6b21\u83b7\u53d6\u6bcf\u7ec4\u5185\u5bb9\u3002\u4ee3\u7801\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\tresults = re.findall(&#8216;&lt;li.*?href=&#8221;(.*?)&#8221;.*?singer=&#8221;(.*?)&#8221;&gt;(.*?)&lt;\/a&gt;&#8217;, html, re.S)\nprint(results)\nprint(type(results))\nfor result in results:\n    print(result)\n    print(result[0], result[1], result[2])\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t123456\n\t\t\t\t<\/td><td>results = re.findall(&#8216;&lt;li.*?href=&#8221;(.*?)&#8221;.*?singer=&#8221;(.*?)&#8221;&gt;(.*?)&lt;\/a&gt;&#8217;, html, re.S)print(results)print(type(results))for result in results:&nbsp;&nbsp;&nbsp;&nbsp;print(result)&nbsp;&nbsp;&nbsp;&nbsp;print(result[0], result[1], result[2])<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t[(&#8216;\/2.mp3&#8217;, &#8216;\u4efb\u8d24\u9f50&#8217;, &#8216;\u6ca7\u6d77\u4e00\u58f0\u7b11&#8217;), (&#8216;\/3.mp3&#8217;, &#8216;\u9f50\u79e6&#8217;, &#8216;\u5f80\u4e8b\u968f\u98ce&#8217;), (&#8216;\/4.mp3&#8217;, &#8216;beyond&#8217;, &#8216;\u5149\u8f89\u5c81\u6708&#8217;), (&#8216;\/5.mp3&#8217;, &#8216;\u9648\u6167\u7433&#8217;, &#8216;\u8bb0\u4e8b\u672c&#8217;), (&#8216;\/6.mp3&#8217;, &#8216;\u9093\u4e3d\u541b&#8217;, &#8216;\u4f46\u613f\u4eba\u957f\u4e45&#8217;)]\n&lt;class &#8216;list&#8217;&gt;\n(&#8216;\/2.mp3&#8217;, &#8216;\u4efb\u8d24\u9f50&#8217;, &#8216;\u6ca7\u6d77\u4e00\u58f0\u7b11&#8217;)\n\/2.mp3 \u4efb\u8d24\u9f50 \u6ca7\u6d77\u4e00\u58f0\u7b11\n(&#8216;\/3.mp3&#8217;, &#8216;\u9f50\u79e6&#8217;, &#8216;\u5f80\u4e8b\u968f\u98ce&#8217;)\n\/3.mp3 \u9f50\u79e6 \u5f80\u4e8b\u968f\u98ce\n(&#8216;\/4.mp3&#8217;, &#8216;beyond&#8217;, &#8216;\u5149\u8f89\u5c81\u6708&#8217;)\n\/4.mp3 beyond \u5149\u8f89\u5c81\u6708\n(&#8216;\/5.mp3&#8217;, &#8216;\u9648\u6167\u7433&#8217;, &#8216;\u8bb0\u4e8b\u672c&#8217;)\n\/5.mp3 \u9648\u6167\u7433 \u8bb0\u4e8b\u672c\n(&#8216;\/6.mp3&#8217;, &#8216;\u9093\u4e3d\u541b&#8217;, &#8216;\u4f46\u613f\u4eba\u957f\u4e45&#8217;)\n\/6.mp3 \u9093\u4e3d\u541b \u4f46\u613f\u4eba\u957f\u4e45\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t123456789101112\n\t\t\t\t<\/td><td>[(&#8216;\/2.mp3&#8217;, &#8216;\u4efb\u8d24\u9f50&#8217;, &#8216;\u6ca7\u6d77\u4e00\u58f0\u7b11&#8217;), (&#8216;\/3.mp3&#8217;, &#8216;\u9f50\u79e6&#8217;, &#8216;\u5f80\u4e8b\u968f\u98ce&#8217;), (&#8216;\/4.mp3&#8217;, &#8216;beyond&#8217;, &#8216;\u5149\u8f89\u5c81\u6708&#8217;), (&#8216;\/5.mp3&#8217;, &#8216;\u9648\u6167\u7433&#8217;, &#8216;\u8bb0\u4e8b\u672c&#8217;), (&#8216;\/6.mp3&#8217;, &#8216;\u9093\u4e3d\u541b&#8217;, &#8216;\u4f46\u613f\u4eba\u957f\u4e45&#8217;)]&lt;class &#8216;list&#8217;&gt;(&#8216;\/2.mp3&#8217;, &#8216;\u4efb\u8d24\u9f50&#8217;, &#8216;\u6ca7\u6d77\u4e00\u58f0\u7b11&#8217;)\/2.mp3 \u4efb\u8d24\u9f50 \u6ca7\u6d77\u4e00\u58f0\u7b11(&#8216;\/3.mp3&#8217;, &#8216;\u9f50\u79e6&#8217;, &#8216;\u5f80\u4e8b\u968f\u98ce&#8217;)\/3.mp3 \u9f50\u79e6 \u5f80\u4e8b\u968f\u98ce(&#8216;\/4.mp3&#8217;, &#8216;beyond&#8217;, &#8216;\u5149\u8f89\u5c81\u6708&#8217;)\/4.mp3 beyond \u5149\u8f89\u5c81\u6708(&#8216;\/5.mp3&#8217;, &#8216;\u9648\u6167\u7433&#8217;, &#8216;\u8bb0\u4e8b\u672c&#8217;)\/5.mp3 \u9648\u6167\u7433 \u8bb0\u4e8b\u672c(&#8216;\/6.mp3&#8217;, &#8216;\u9093\u4e3d\u541b&#8217;, &#8216;\u4f46\u613f\u4eba\u957f\u4e45&#8217;)\/6.mp3 \u9093\u4e3d\u541b \u4f46\u613f\u4eba\u957f\u4e45<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8fd4\u56de\u7684\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u5143\u7ec4\u7c7b\u578b\uff0c\u6211\u4eec\u7528\u5bf9\u5e94\u7684\u7d22\u5f15\u4f9d\u6b21\u53d6\u51fa\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u53ea\u662f\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5185\u5bb9\uff0c\u53ef\u4ee5\u7528<code>search()<\/code>\u65b9\u6cd5\u3002\u5f53\u9700\u8981\u63d0\u53d6\u591a\u4e2a\u5185\u5bb9\u65f6\uff0c\u53ef\u4ee5\u7528<code>findall()<\/code>\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.&nbsp;<code>sub()<\/code><\/h2>\n\n\n\n<p>\u9664\u4e86\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u4fe1\u606f\u5916\uff0c\u6709\u65f6\u5019\u8fd8\u9700\u8981\u501f\u52a9\u5b83\u6765\u4fee\u6539\u6587\u672c\u3002\u6bd4\u5982\uff0c\u60f3\u8981\u628a\u4e00\u4e32\u6587\u672c\u4e2d\u7684\u6240\u6709\u6570\u5b57\u90fd\u53bb\u6389\uff0c\u5982\u679c\u53ea\u7528\u5b57\u7b26\u4e32\u7684<code>replace()<\/code>\u65b9\u6cd5\uff0c\u90a3\u5c31\u592a\u70e6\u7410\u4e86\uff0c\u8fd9\u65f6\u53ef\u4ee5\u501f\u52a9<code>sub()<\/code>\u65b9\u6cd5\u3002\u793a\u4f8b\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\timport re\n\ncontent = &#8217;54aK54yr5oiR54ix5L2g&#8217;\ncontent = re.sub(&#8216;\\d+&#8217;, &#8221;, content)\nprint(content)\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t12345\n\t\t\t\t<\/td><td>import re&nbsp;content = &#8217;54aK54yr5oiR54ix5L2g&#8217;content = re.sub(&#8216;\\d+&#8217;, &#8221;, content)print(content)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\taKyroiRixLg\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>aKyroiRixLg<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u53ea\u9700\u8981\u7ed9\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20\u5165<code>\\d+<\/code>\u6765\u5339\u914d\u6240\u6709\u7684\u6570\u5b57\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u66ff\u6362\u6210\u7684\u5b57\u7b26\u4e32\uff08\u5982\u679c\u53bb\u6389\u8be5\u53c2\u6570\u7684\u8bdd\uff0c\u53ef\u4ee5\u8d4b\u503c\u4e3a\u7a7a\uff09\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u539f\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684HTML\u6587\u672c\u4e2d\uff0c\u5982\u679c\u60f3\u83b7\u53d6\u6240\u6709<code>li<\/code>\u8282\u70b9\u7684\u6b4c\u540d\uff0c\u76f4\u63a5\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u53ef\u80fd\u6bd4\u8f83\u70e6\u7410\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u5199\u6210\u8fd9\u6837\u5b50\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\tresults = re.findall(&#8216;&lt;li.*?&gt;\\s*?(&lt;a.*?&gt;)?(\\w+)(&lt;\/a&gt;)?\\s*?&lt;\/li&gt;&#8217;, html, re.S)\nfor result in results:\n    print(result[1])\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t123\n\t\t\t\t<\/td><td>results = re.findall(&#8216;&lt;li.*?&gt;\\s*?(&lt;a.*?&gt;)?(\\w+)(&lt;\/a&gt;)?\\s*?&lt;\/li&gt;&#8217;, html, re.S)for result in results:&nbsp;&nbsp;&nbsp;&nbsp;print(result[1])<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\u4e00\u8def\u4e0a\u6709\u4f60\n\u6ca7\u6d77\u4e00\u58f0\u7b11\n\u5f80\u4e8b\u968f\u98ce\n\u5149\u8f89\u5c81\u6708\n\u8bb0\u4e8b\u672c\n\u4f46\u613f\u4eba\u957f\u4e45\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t123456\n\t\t\t\t<\/td><td>\u4e00\u8def\u4e0a\u6709\u4f60\u6ca7\u6d77\u4e00\u58f0\u7b11\u5f80\u4e8b\u968f\u98ce\u5149\u8f89\u5c81\u6708\u8bb0\u4e8b\u672c\u4f46\u613f\u4eba\u957f\u4e45<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6b64\u65f6\u501f\u52a9<code>sub()<\/code>\u65b9\u6cd5\u5c31\u6bd4\u8f83\u7b80\u5355\u4e86\u3002\u53ef\u4ee5\u5148\u7528<code>sub()<\/code>\u65b9\u6cd5\u5c06<code>a<\/code>\u8282\u70b9\u53bb\u6389\uff0c\u53ea\u7559\u4e0b\u6587\u672c\uff0c\u7136\u540e\u518d\u5229\u7528<code>findall()<\/code>\u63d0\u53d6\u5c31\u597d\u4e86\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\thtml = re.sub(&#8216;&lt;a.*?&gt;|&lt;\/a&gt;&#8217;, &#8221;, html)\nprint(html)\nresults = re.findall(&#8216;&lt;li.*?&gt;(.*?)&lt;\/li&gt;&#8217;, html, re.S)\nfor result in results:\n    print(result.strip())\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t12345\n\t\t\t\t<\/td><td>html = re.sub(&#8216;&lt;a.*?&gt;|&lt;\/a&gt;&#8217;, &#8221;, html)print(html)results = re.findall(&#8216;&lt;li.*?&gt;(.*?)&lt;\/li&gt;&#8217;, html, re.S)for result in results:&nbsp;&nbsp;&nbsp;&nbsp;print(result.strip())<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t&lt;div id=&#8221;songs-list&#8221;&gt;\n    &lt;h2 class=&#8221;title&#8221;&gt;\u7ecf\u5178\u8001\u6b4c&lt;\/h2&gt;\n    &lt;p class=&#8221;introduction&#8221;&gt;\n        \u7ecf\u5178\u8001\u6b4c\u5217\u8868\n    &lt;\/p&gt;\n    &lt;ul id=&#8221;list&#8221; class=&#8221;list-group&#8221;&gt;\n        &lt;li data-view=&#8221;2&#8243;&gt;\u4e00\u8def\u4e0a\u6709\u4f60&lt;\/li&gt;\n        &lt;li data-view=&#8221;7&#8243;&gt;\n            \u6ca7\u6d77\u4e00\u58f0\u7b11\n        &lt;\/li&gt;\n        &lt;li data-view=&#8221;4&#8243; class=&#8221;active&#8221;&gt;\n            \u5f80\u4e8b\u968f\u98ce\n        &lt;\/li&gt;\n        &lt;li data-view=&#8221;6&#8243;&gt;\u5149\u8f89\u5c81\u6708&lt;\/li&gt;\n        &lt;li data-view=&#8221;5&#8243;&gt;\u8bb0\u4e8b\u672c&lt;\/li&gt;\n        &lt;li data-view=&#8221;5&#8243;&gt;\n            \u4f46\u613f\u4eba\u957f\u4e45\n        &lt;\/li&gt;\n    &lt;\/ul&gt;\n&lt;\/div&gt;\n\u4e00\u8def\u4e0a\u6709\u4f60\n\u6ca7\u6d77\u4e00\u58f0\u7b11\n\u5f80\u4e8b\u968f\u98ce\n\u5149\u8f89\u5c81\u6708\n\u8bb0\u4e8b\u672c\n\u4f46\u613f\u4eba\u957f\u4e45\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1234567891011121314151617181920212223242526\n\t\t\t\t<\/td><td>&lt;div id=&#8221;songs-list&#8221;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2 class=&#8221;title&#8221;&gt;\u7ecf\u5178\u8001\u6b4c&lt;\/h2&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p class=&#8221;introduction&#8221;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u7ecf\u5178\u8001\u6b4c\u5217\u8868&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/p&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;ul id=&#8221;list&#8221; class=&#8221;list-group&#8221;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;2&#8243;&gt;\u4e00\u8def\u4e0a\u6709\u4f60&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;7&#8243;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u6ca7\u6d77\u4e00\u58f0\u7b11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;4&#8243; class=&#8221;active&#8221;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u5f80\u4e8b\u968f\u98ce&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;6&#8243;&gt;\u5149\u8f89\u5c81\u6708&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;5&#8243;&gt;\u8bb0\u4e8b\u672c&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li data-view=&#8221;5&#8243;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u4f46\u613f\u4eba\u957f\u4e45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/li&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/ul&gt;&lt;\/div&gt;\u4e00\u8def\u4e0a\u6709\u4f60\u6ca7\u6d77\u4e00\u58f0\u7b11\u5f80\u4e8b\u968f\u98ce\u5149\u8f89\u5c81\u6708\u8bb0\u4e8b\u672c\u4f46\u613f\u4eba\u957f\u4e45<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<code>a<\/code>\u8282\u70b9\u7ecf\u8fc7<code>sub()<\/code>\u65b9\u6cd5\u5904\u7406\u540e\u5c31\u6ca1\u6709\u4e86\uff0c\u7136\u540e\u518d\u901a\u8fc7<code>findall()<\/code>\u65b9\u6cd5\u76f4\u63a5\u63d0\u53d6\u5373\u53ef\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u9002\u5f53\u7684\u65f6\u5019\uff0c\u501f\u52a9<code>sub()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8d77\u5230\u4e8b\u534a\u529f\u500d\u7684\u6548\u679c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6.&nbsp;<code>compile()<\/code><\/h2>\n\n\n\n<p>\u524d\u9762\u6240\u8bb2\u7684\u65b9\u6cd5\u90fd\u662f\u7528\u6765\u5904\u7406\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u6700\u540e\u518d\u4ecb\u7ecd\u4e00\u4e0b<code>compile()<\/code>\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6b63\u5219\u5b57\u7b26\u4e32\u7f16\u8bd1\u6210\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u5728\u540e\u9762\u7684\u5339\u914d\u4e2d\u590d\u7528\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\timport re\n\ncontent1 = &#8216;2016-12-15 12:00&#8217;\ncontent2 = &#8216;2016-12-17 12:55&#8217;\ncontent3 = &#8216;2016-12-22 13:21&#8217;\npattern = re.compile(&#8216;\\d{2}:\\d{2}&#8217;)\nresult1 = re.sub(pattern, &#8221;, content1)\nresult2 = re.sub(pattern, &#8221;, content2)\nresult3 = re.sub(pattern, &#8221;, content3)\nprint(result1, result2, result3)\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t12345678910\n\t\t\t\t<\/td><td>import re&nbsp;content1 = &#8216;2016-12-15 12:00&#8217;content2 = &#8216;2016-12-17 12:55&#8217;content3 = &#8216;2016-12-22 13:21&#8217;pattern = re.compile(&#8216;\\d{2}:\\d{2}&#8217;)result1 = re.sub(pattern, &#8221;, content1)result2 = re.sub(pattern, &#8221;, content2)result3 = re.sub(pattern, &#8221;, content3)print(result1, result2, result3)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4f8b\u5982\uff0c\u8fd9\u91cc\u67093\u4e2a\u65e5\u671f\uff0c\u6211\u4eec\u60f3\u5206\u522b\u5c063\u4e2a\u65e5\u671f\u4e2d\u7684\u65f6\u95f4\u53bb\u6389\uff0c\u8fd9\u65f6\u53ef\u4ee5\u501f\u52a9<code>sub()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f46\u662f\u8fd9\u91cc\u6ca1\u6709\u5fc5\u8981\u91cd\u590d\u51993\u4e2a\u540c\u6837\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6b64\u65f6\u53ef\u4ee5\u501f\u52a9<code>compile()<\/code>\u65b9\u6cd5\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u8bd1\u6210\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u590d\u7528\u3002<\/p>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n\n\n\t\t\n\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t2016-12-15  2016-12-17  2016-12-22\n\t\t\t\n\t\t\t\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td>\n\t\t\t\t\t1\n\t\t\t\t<\/td><td>2016-12-15&nbsp;&nbsp;2016-12-17&nbsp;&nbsp;2016-12-22<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53e6\u5916\uff0c<code>compile()<\/code>\u8fd8\u53ef\u4ee5\u4f20\u5165\u4fee\u9970\u7b26\uff0c\u4f8b\u5982<code>re.S<\/code>\u7b49\u4fee\u9970\u7b26\uff0c\u8fd9\u6837\u5728<code>search()<\/code>\u3001<code>findall()<\/code>\u7b49\u65b9\u6cd5\u4e2d\u5c31\u4e0d\u9700\u8981\u989d\u5916\u4f20\u4e86\u3002\u6240\u4ee5\uff0c<code>compile()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bf4\u662f\u7ed9\u6b63\u5219\u8868\u8fbe\u5f0f\u505a\u4e86\u4e00\u5c42\u5c01\u88c5\uff0c\u4ee5\u4fbf\u6211\u4eec\u66f4\u597d\u5730\u590d\u7528\u3002<\/p>\n\n\n\n<p>\u5230\u6b64\u4e3a\u6b62\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u7528\u6cd5\u5c31\u4ecb\u7ecd\u5b8c\u4e86\uff0c\u540e\u9762\u4f1a\u901a\u8fc7\u5177\u4f53\u7684\u5b9e\u4f8b\u6765\u8bb2\u89e3\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/cuiqingcai.com\">\u9759\u89c5<\/a> \u00bb <a href=\"https:\/\/cuiqingcai.com\/5530.html\">[Python3\u7f51\u7edc\u722c\u866b\u5f00\u53d1\u5b9e\u6218] 3.3-\u6b63\u5219\u8868\u8fbe\u5f0f<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5b9e\u4f8b\u5f15\u5165 \u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u53ef\u80fd\u6211\u4eec\u5bf9\u5b83\u5230\u5e95\u662f\u4e2a\u4ec0\u4e48\u8fd8\u662f\u6bd4\u8f83\u6a21\u7cca\uff0c\u4e0b\u9762\u5c31\u7528\u51e0\u4e2a\u5b9e <span class=\"readmore\"><a href=\"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=591\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-591","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=591"}],"version-history":[{"count":1,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/591\/revisions"}],"predecessor-version":[{"id":592,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/591\/revisions\/592"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=591"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}