{"id":39261,"date":"2025-05-23T09:48:47","date_gmt":"2025-05-23T01:48:47","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/39261.html"},"modified":"2025-05-23T09:48:47","modified_gmt":"2025-05-23T01:48:47","slug":"%e3%80%8a100%e5%a4%a9%e7%b2%be%e9%80%9apython-%e5%9f%ba%e7%a1%80%e7%af%87-2025-%e7%ac%ac18%e5%a4%a9%ef%bc%9a%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%85%a5%e9%97%a8%e5%ae%9e","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/39261.html","title":{"rendered":"\u300a100\u5929\u7cbe\u901aPython\u2014\u2014\u57fa\u7840\u7bc7 2025 \u7b2c18\u5929\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5165\u95e8\u5b9e\u6218\uff0c\u89e3\u9501\u5b57\u7b26\u4e32\u5904\u7406\u7684\u9b54\u6cd5\u529b\u91cf\u300b"},"content":{"rendered":"<\/p>\n<h4>\u76ee\u5f55<\/h4>\n<ul>\n<li>\u4e00\u3001\u8ba4\u8bc6\u6b63\u5219\u8868\u8fbe\u5f0f<\/li>\n<li>\u4e8c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u57fa\u672c\u8bed\u6cd5<\/li>\n<li>\n<ul>\n<li>2.1 \u884c\u754c\u5b9a\u7b26<\/li>\n<li>2.2 \u5355\u8bcd\u5b9a\u754c\u7b26<\/li>\n<li>2.3 \u5b57\u7b26\u7c7b<\/li>\n<li>2.4 \u9009\u62e9\u7b26<\/li>\n<li>2.5 \u8303\u56f4\u7b26<\/li>\n<li>2.6 \u6392\u9664\u7b26<\/li>\n<li>2.7 \u9650\u5b9a\u7b26<\/li>\n<li>2.8 \u4efb\u610f\u5b57\u7b26<\/li>\n<li>2.9 \u8f6c\u4e49\u5b57\u7b26<\/li>\n<li>2.10 \u53cd\u659c\u6760<\/li>\n<li>2.11 \u5c0f\u62ec\u53f7<\/li>\n<li>\n<ul>\n<li>2.11.1 \u5b9a\u4e49\u72ec\u7acb\u5355\u5143<\/li>\n<li>2.11.2 \u5206\u7ec4<\/li>\n<\/ul>\n<\/li>\n<li>2.12 \u53cd\u5411\u5f15\u7528<\/li>\n<li>2.13 \u7279\u6b8a\u6784\u9020<\/li>\n<li>2.14 \u5339\u914d\u6a21\u5f0f<\/li>\n<\/ul>\n<\/li>\n<li>\u4e09\u3001re\u6a21\u5757<\/li>\n<li>\n<ul>\n<li>3.1 compile()\u65b9\u6cd5\u2014\u2014\u7f16\u8bd1\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61<\/li>\n<li>3.2 escape()\u65b9\u6cd5\u2014\u2014\u8f6c\u4e49\u7279\u6b8a\u5b57\u7b26<\/li>\n<li>3.3 f\u200aindall()\u65b9\u6cd5\u2014\u2014\u641c\u7d22\u6240\u6709\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\u4e32<\/li>\n<li>3.4 f\u200ainditer()\u65b9\u6cd5\u2014\u2014\u627e\u5230\u6240\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32<\/li>\n<li>3.5 fullmatch()\u65b9\u6cd5\u2014\u2014\u5bf9\u6574\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d<\/li>\n<li>3.6 match()\u65b9\u6cd5\u2014\u2014\u4ece\u5b57\u7b26\u5f00\u59cb\u5904\u8fdb\u884c\u5339\u914d<\/li>\n<li>3.7 search()\u65b9\u6cd5\u2014\u2014\u641c\u7d22\u7b2c\u4e00\u4e2a\u5339\u914d\u4f4d\u7f6e<\/li>\n<li>3.8 split()\u65b9\u6cd5\u2014\u2014\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u5b57\u7b26\u4e32<\/li>\n<li>3.9 sub()\u65b9\u6cd5\u2014\u2014\u5b57\u7b26\u4e32\u66ff\u6362<\/li>\n<li>3.10 subn()\u65b9\u6cd5\u2014\u2014\u5b57\u7b26\u4e32\u66ff\u6362 &#xff08;\u542b\u66ff\u6362\u6b21\u6570&#xff09;<\/li>\n<li>3.11 Pattern\u7684\u5c5e\u6027\u4ee5\u53ca\u65b9\u6cd5<\/li>\n<li>\n<ul>\n<li>3.11.1 Pattern.flags\u5c5e\u6027\u2014\u2014\u83b7\u53d6\u6b63\u5219\u5339\u914d\u6807\u8bb0<\/li>\n<li>3.11.2 Pattern.groups\u5c5e\u6027\u2014\u2014\u6355\u83b7\u7ec4\u5408\u7684\u6570\u91cf<\/li>\n<li>3.11.3 Pattern.groupindex\u5c5e\u6027\u2014\u2014\u83b7\u53d6\u5206\u7ec4\u540d\u79f0\u4e0e\u5bf9\u5e94\u7684\u5206\u7ec4\u7f16\u53f7<\/li>\n<li>3.11.4 Pattern.pattern\u5c5e\u6027\u2014\u2014\u83b7\u53d6\u6a21\u5f0f\u5b57\u7b26\u4e32<\/li>\n<li>3.11.5 Pattern.f\u200aindall()\u65b9\u6cd5\u2014\u2014\u641c\u7d22\u6240\u6709\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\u4e32<\/li>\n<li>3.11.6 Pattern.f\u200ainditer()\u65b9\u6cd5\u2014\u2014\u641c\u7d22\u6240\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32<\/li>\n<li>3.11.7 Pattern.fullmatch()\u65b9\u6cd5\u2014\u2014\u5bf9\u6574\u4e2a\u5b57\u7b26\u4e32\u5339\u914d<\/li>\n<li>3.11.8 Pattern.match()\u65b9\u6cd5\u2014\u2014\u4ece\u5b57\u7b26\u5f00\u59cb\u5904\u5339\u914d\u5b57\u7b26\u4e32\u7684\u503c<\/li>\n<li>3.11.9 Pattern.search()\u65b9\u6cd5\u2014\u2014\u641c\u7d22\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u4f4d\u7f6e<\/li>\n<li>3.11.10 Pattern.split()\u65b9\u6cd5\u2014\u2014\u540csplit()\u65b9\u6cd5<\/li>\n<li>3.11.11 Pattern.sub()\u65b9\u6cd5\u2014\u2014\u5b57\u7b26\u4e32\u66ff\u6362<\/li>\n<li>3.11.12 Pattern.subn()\u65b9\u6cd5\u2014\u2014\u5b57\u7b26\u4e32\u66ff\u6362&#xff08;\u542b\u66ff\u6362\u6b21\u6570&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>3.12 Match\u5bf9\u8c61\u5c5e\u6027\u53ca\u65b9\u6cd5<\/li>\n<li>\n<ul>\n<li>3.12.1 Match.endpos\u5c5e\u6027\u2014\u2014\u7ed3\u675f\u5339\u914d\u7684\u7d22\u5f15\u4f4d\u7f6e<\/li>\n<li>3.12.2 Match.pos\u5c5e\u6027\u2014\u2014\u5f00\u59cb\u5339\u914d\u7684\u7d22\u5f15\u4f4d\u7f6e<\/li>\n<li>3.12.3 Match.lastindex\u5c5e\u6027\u2014\u2014\u6355\u83b7\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u5339\u914d\u7684\u7d22\u5f15\u503c<\/li>\n<li>3.12.4 Match.lastgroup\u5c5e\u6027\u2014\u2014\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5339\u914d\u7ec4\u7684\u540d\u79f0<\/li>\n<li>3.12.5 Match.re\u5c5e\u6027\u2014\u2014\u8fd4\u56de\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61<\/li>\n<li>3.12.6 Match.string\u5c5e\u6027\u2014\u2014\u83b7\u53d6\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/li>\n<li>3.12.7 Match.&#096;__getitem__()&#096; \u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u7684\u5206\u7ec4\u4fe1\u606f<\/li>\n<li>3.12.8 Match.end()\u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u7ed3\u675f\u6807\u8bb0<\/li>\n<li>3.12.9 Match.expand()\u65b9\u6cd5\u2014\u2014\u5bf9\u201c\\\\\u201d\u8fdb\u884c\u8f6c\u4e49\u66ff\u6362\u5e76\u8fd4\u56de<\/li>\n<li>3.12.10 Match.groups()\u65b9\u6cd5\u2014\u2014\u4ee5\u5143\u7ec4\u5f62\u5f0f\u8fd4\u56de\u6240\u6709\u5b50\u7ec4\u4e2d\u5185\u5bb9<\/li>\n<li>3.12.11 Match.group()\u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u5339\u914d\u7684\u5b50\u7ec4<\/li>\n<li>3.12.12 Match.groupdict()\u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u5305\u542b\u6240\u6709\u5339\u914d\u503c\u5206\u7ec4\u7684\u5b57\u5178<\/li>\n<li>3.12.13 Match.span()\u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u5305\u542b\u5f00\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\u7684\u4e8c\u5143\u7ec4<\/li>\n<li>3.12.14 Match.start()\u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u6807\u8bb0<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u56db\u3001\u7ec3\u4e60<\/li>\n<\/ul>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u975e\u5e38\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u5de5\u5177&#xff0c;\u5176\u8bed\u6cd5\u5f62\u5f0f\u4e3a\u4e00\u4e2a\u7279\u6b8a\u7684\u5b57\u7b26\u5e8f\u5217&#xff0c;\u5e38\u7528\u6765\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d\u64cd\u4f5c\u3002Python \u4ece 1.5 \u7248\u672c\u5f00\u59cb\u65b0\u589e re \u6a21\u5757&#xff0c;\u63d0\u4f9b Perl \u98ce\u683c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5&#xff0c;\u4ee5\u53ca Python \u6b63\u5219\u8868\u8fbe\u5f0f\u6807\u51c6\u5e93\u7684\u57fa\u672c\u7528\u6cd5\u3002<\/p>\n<p>\u3010\u5b66\u4e60\u91cd\u70b9\u3011<\/p>\n<li>\u4e86\u89e3\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u76f8\u5173\u6982\u5ff5\u3002<\/li>\n<li>\u638c\u63e1\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u3002<\/li>\n<li>\u719f\u6089 Python \u7684 re \u6a21\u5757\u3002<\/li>\n<li>\u80fd\u591f\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/li>\n<h2>\u4e00\u3001\u8ba4\u8bc6\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53c8\u79f0\u89c4\u5219\u8868\u8fbe\u5f0f&#xff08;Regular Expression&#xff09;&#xff0c;\u5728\u4ee3\u7801\u4e2d\u7b80\u5199\u4e3a regex\u3001regexp \u6216 RE&#xff0c;\u901a\u5e38\u88ab\u7528\u6765\u5339\u914d\u7b26\u5408\u6307\u5b9a\u6a21\u5f0f&#xff08;\u89c4\u5219&#xff09;\u7684\u6587\u672c\u3002\u73b0\u4ee3\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\u90fd\u652f\u6301\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002\u5b9e\u9645\u4e0a&#xff0c;\u6b63\u5219\u8868\u8fbe\u5f0f\u5c31\u662f\u4e00\u79cd\u903b\u8f91\u6a21\u677f&#xff0c;\u662f\u7528\u4e8b\u5148\u5b9a\u4e49\u597d\u7684\u4e00\u7ec4\u7279\u5b9a\u5b57\u7b26&#xff0c;\u4ee5\u53ca\u8fd9\u4e9b\u7279\u5b9a\u5b57\u7b26\u7684\u4efb\u610f\u7ec4\u5408&#xff0c;\u7ec4\u6210\u4e00\u4e2a &#034;\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32&#034;&#xff0c; \u8fd9\u4e2a &#034;\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32&#034; \u7528\u6765\u8868\u8fbe\u5bf9\u5b57\u7b26\u4e32\u7684\u4e00\u79cd\u8fc7\u6ee4\u903b\u8f91\u3002<\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u4e00\u4e2a\u88ab\u64cd\u4f5c\u7684\u5b57\u7b26\u4e32&#xff0c;\u53ef\u4ee5\u8fbe\u5230\u4ee5\u4e0b\u76ee\u7684&#xff1a;<\/p>\n<li>\u9a8c\u8bc1\u88ab\u64cd\u4f5c\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u903b\u8f91\u3002<\/li>\n<li>\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f&#xff0c;\u4ece\u88ab\u64cd\u4f5c\u5b57\u7b26\u4e32\u4e2d\u83b7\u53d6\u7279\u5b9a\u7684\u4fe1\u606f&#xff0c;\u6216\u8005\u4fee\u6539\u5b57\u7b26\u4e32\u3002<\/li>\n<p>Python \u652f\u6301 Perl \u98ce\u683c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u3002\u4e0b\u9762\u5148\u4e86\u89e3\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u76f8\u5173\u7684\u51e0\u4e2a\u6982\u5ff5&#xff1a;<\/p>\n<li>grep&#xff1a;grep \u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u641c\u7d22\u5de5\u5177&#xff0c;\u5b83\u80fd\u4f7f\u7528\u7279\u5b9a\u6a21\u5f0f\u5339\u914d&#xff08;\u5305\u62ec\u6b63\u5219\u8868\u8fbe\u5f0f&#xff09;\u641c\u7d22\u6587\u672c&#xff0c;\u5e76\u9ed8\u8ba4\u8f93\u51fa\u5339\u914d\u884c\u3002<\/li>\n<li>egrep&#xff1a;\u7531\u4e8e grep \u66f4\u65b0\u7684\u901f\u5ea6\u65e0\u6cd5\u4e0e\u6280\u672f\u66f4\u65b0\u7684\u901f\u5ea6\u540c\u6b65\u3002\u4e3a\u6b64&#xff0c;\u8d1d\u5c14\u5b9e\u9a8c\u5ba4\u63a8\u51fa\u4e86 egrep&#xff0c;\u5373\u6269\u5c55\u7684 grep&#xff0c;\u8fd9\u5927\u5927\u589e\u5f3a\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u80fd\u529b\u3002<\/li>\n<li>POSIX&#xff1a;\u5728 grep \u53d1\u5c55\u7684\u540c\u65f6&#xff0c;\u5176\u4ed6\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u4e5f\u6839\u636e\u81ea\u5df1\u7684\u559c\u597d\u5f00\u53d1\u51fa\u4e86\u5177\u6709\u72ec\u7279\u98ce\u683c\u7684\u7248\u672c\u3002\u4f46\u95ee\u9898\u4e5f\u968f\u4e4b\u800c\u6765&#xff0c;\u6709\u7684\u7a0b\u5e8f\u652f\u6301\u67d0\u4e2a\u5143\u5b57\u7b26&#xff0c;\u800c\u6709\u7684\u7a0b\u5e8f\u5219\u4e0d\u652f\u6301&#xff0c;\u56e0\u6b64\u5c31\u6709\u4e86 POSIX\u3002POSIX \u662f\u4e00\u7cfb\u5217\u6807\u51c6&#xff0c;\u786e\u4fdd\u4e86\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u7684\u53ef\u79fb\u690d\u6027\u3002\u4f46 POSIX \u548c SQL \u4e00\u6837\u6ca1\u6709\u6210\u4e3a\u6700\u7ec8\u7684\u6807\u51c6&#xff0c;\u800c\u53ea\u80fd\u4f5c\u4e3a\u4e00\u4e2a\u53c2\u8003\u3002<\/li>\n<li>Perl: 1987\u5e74&#xff0c;Larry Wall \u53d1\u5e03\u4e86 Perl \u7f16\u7a0b\u8bed\u8a00&#xff0c;\u5b83\u6c72\u53d6\u4e86\u591a\u79cd\u8bed\u8a00\u7cbe\u534e&#xff0c;\u5e76\u5185\u90e8\u96c6\u6210\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u529f\u80fd&#xff0c;\u4ee5\u53ca\u5de8\u5927\u7684\u7b2c\u4e09\u65b9\u4ee3\u7801\u5e93 CPAN\u3002Perl \u7ecf\u5386\u4e86\u4ece Perl 1 \u5230\u73b0\u5728 Perl 6 \u7684\u53d1\u5c55\u6700\u7ec8\u6210\u4e86 POSIX \u4e4b\u540e\u7684\u53e6\u4e00\u4e2a\u6807\u51c6\u3002<\/li>\n<li>PCRE&#xff1a;1997 \u5e74&#xff0c;Philip Hazel \u5f00\u53d1\u4e86 PCRE \u5e93&#xff0c;\u5b83\u662f\u80fd\u591f\u517c\u5bb9 Perl \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e00\u5957\u6b63\u5219\u5f15\u64ce&#xff0c;\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5c06 PCRE \u6574\u5408\u5230\u81ea\u5df1\u7684\u8bed\u8a00\u4e2d&#xff0c;\u4e3a\u7528\u6237\u63d0\u4f9b\u4e30\u5bcc\u7684\u6b63\u5219\u529f\u80fd\u3002<\/li>\n<p>\u53c2\u8003&#xff1a;https:\/\/www.w3cschool.cn\/regex_rmjc\/ \u5206\u7c7b&#xff1a;<\/p>\n<p>1.BRE\u3002\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f&#xff0c;grep\u3001sed\u3001vi\u7b49\u8f6f\u4ef6\u652f\u6301\u3002vim\u6709\u6269\u5c55\u3002<br \/>\n2.ERE\u3002\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f&#xff0c;egrep&#xff08;grep -E&#xff09;\u3001sed -r\u7b49\u3002<br \/>\n3.PCRE\u3002\u51e0\u4e4e\u6240\u6709\u9ad8\u7ea7\u8bed\u8a00\u90fd\u662fPCRE\u7684\u65b9\u8a00\u6216\u8005\u53d8\u79cd\u3002Python\u4ece1.6\u5f00\u59cb\u4f7f\u7528SRE\u6b63\u5219\u8868\u8fbe\u5f0f\u5f15\u64ce&#xff0c;\u53ef\u4ee5\u8ba4\u4e3a\u662fPCRE\u7684\u5b50\u96c6&#xff0c;\u89c1\u6a21\u5757re\u3002<\/p>\n<h2>\u4e8c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u57fa\u672c\u8bed\u6cd5<\/h2>\n<p>\u5728\u5f62\u5f0f\u8bed\u8a00\u7406\u8bba\u4e2d&#xff0c;\u6b63\u5219\u8868\u8fbe\u5f0f\u88ab\u79f0\u4e3a &#034;\u89c4\u5219\u8bed\u8a00&#034;&#xff0c;\u800c\u4e0d\u662f\u7f16\u7a0b\u8bed\u8a00\u3002\u5728 Python \u4e2d&#xff0c;&#034;\u6b63\u5219\u8868\u8fbe\u5f0f&#034; \u8fd9\u4e2a\u6982\u5ff5\u5305\u542b\u4e24\u5c42\u542b\u4e49&#xff1a;\u4e00\u4e2a\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32&#xff1b;\u53e6\u4e00\u4e2a\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6587\u672c\u8868\u793a&#xff0c;\u5b83\u7ecf\u8fc7\u7f16\u8bd1\u540e\u5c31\u6210\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u5177\u6709\u5339\u914d\u529f\u80fd\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32\u7531\u4e24\u90e8\u5206\u6784\u6210&#xff1a;\u5143\u5b57\u7b26\u548c\u666e\u901a\u5b57\u7b26\u3002\u5143\u5b57\u7b26\u662f\u5177\u6709\u7279\u6b8a\u542b\u4e49\u7684\u5b57\u7b26&#xff0c;\u5982 &#034;.&#034; \u548c &#034;?&#034;&#xff0c;\u666e\u901a\u5b57\u7b26\u662f\u4ec5\u6307\u4ee3\u81ea\u8eab\u7684\u666e\u901a\u5b57\u7b26&#xff0c;\u5982\u6570\u5b57\u3001\u5b57\u6bcd\u7b49\u3002\u672c\u5c0f\u8282\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5404\u79cd\u5143\u5b57\u7b26\u7684\u542b\u4e49\u548c\u7528\u6cd5\u3002<\/p>\n<p>\u7531\u4e8e\u5728\u8fd9\u91cc\u6211\u4eec\u8fd8\u6ca1\u6709\u5b66\u4e60 re \u6a21\u5757&#xff0c;\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5de5\u5177\u6765\u5b66\u4e60\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5&#xff0c;\u5982\u4e0b\u56fe\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014844-682fd3fcdc750.png\" width=\"50%\" \/> \u8f6f\u4ef6\u4e0b\u8f7d\u5730\u5740&#xff1a;<\/p>\n<p>\u901a\u8fc7\u7f51\u76d8\u5206\u4eab\u7684\u6587\u4ef6&#xff1a;\u6b63\u5219\u8868\u8fbe\u5f0f<br \/>\n\u94fe\u63a5: https:\/\/pan.baidu.com\/s\/1TeYRnx1QkgHY6rfxlSQ9Jw?pwd&#061;1946 \u63d0\u53d6\u7801: 1946<br \/>\n&#8211;\u6765\u81ea\u767e\u5ea6\u7f51\u76d8\u8d85\u7ea7\u4f1a\u5458v9\u7684\u5206\u4eab<\/p>\n<p>\u7f51\u76d8\u4e2d\u5e26\u6709\u4e00\u4e9b\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6587\u6863\u8d44\u6599&#xff0c;\u4e0d\u9700\u8981\u53ef\u4ee5\u76f4\u63a5\u4e0b\u8f7d \u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u5199\u53ca\u8c03\u8bd5\u5de5\u5177.msi \u8f6f\u4ef6<\/p>\n<h3>2.1 \u884c\u754c\u5b9a\u7b26<\/h3>\n<p>\u884c\u754c\u5b9a\u7b26\u63cf\u8ff0\u4e00\u884c\u5b57\u7b26\u4e32\u7684\u8fb9\u754c&#xff0c;\u5177\u4f53\u8bf4\u660e\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token operator\">^<\/span><span class=\"token punctuation\">:<\/span> \u8868\u793a\u884c\u7684\u5f00\u59cb<br \/>\n$<span class=\"token punctuation\">:<\/span> \u8868\u793a\u884c\u7684\u7ed3\u5c3e<br \/>\n\u5728\u591a\u884c\u5339\u914d\u6a21\u5f0f\u4e2d&#xff0c;\u884c\u5b9a\u754c\u7b26\u80fd\u591f\u5339\u914d\u6bcf\u4e00\u884c\u7684\u884c\u9996\u548c\u884c\u5c3e\u4f4d\u7f6e\u3002<\/p>\n<p>\u2460 \u5355\u884c\u6a21\u5f0f<span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">.<\/span>\u53ef\u4ee5\u5339\u914d\u6240\u6709\u5b57\u7b26&#xff0c;\u5305\u62ec\u6362\u884c\u7b26&#xff0c;<span class=\"token operator\">^<\/span> \u8868\u793a\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u5f00\u5934&#xff0c;$\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e<br \/>\n\u2461 \u591a\u884c\u6a21\u5f0f<span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">.<\/span>\u53ef\u4ee5\u5339\u914d\u9664\u4e86\u6362\u884c\u7b26\u4e4b\u5916\u7684\u5b57\u7b26&#xff0c;\u591a\u884c\u4e0d\u5f71\u54cd<span class=\"token punctuation\">.<\/span>\u70b9\u53f7\u3002<span class=\"token operator\">^<\/span> \u8868\u793a\u884c\u9996&#xff0c;$\u884c\u5c3e&#xff0c;\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u7684\u884c\u662f\u6bcf\u4e00\u4e2a\u884c \u21d2 re<span class=\"token punctuation\">.<\/span>MULTILINE<br \/>\n\u2462 \u9ed8\u8ba4\u6a21\u5f0f<span class=\"token punctuation\">:<\/span> \u53ef\u4ee5\u770b\u505a\u5f85\u5339\u914d\u7684\u6587\u672c\u662f\u4e00\u884c&#xff0c;\u4e0d\u80fd\u770b\u505a\u591a\u884c&#xff0c;<span class=\"token punctuation\">.<\/span>\u70b9\u53f7\u4e0d\u80fd\u5339\u914d\u6362\u884c\u7b26&#xff0c;<span class=\"token operator\">^<\/span>\u548c$\u8868\u793a\u884c\u9996\u548c\u884c\u5c3e&#xff0c;<br \/>\n\u800c\u884c\u9996\u884c\u5c3e\u5c31\u662f\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u548c\u7ed3\u5c3e<br \/>\n\u2463 \u5355\u884c\u6a21\u5f0f<span class=\"token punctuation\">:<\/span> \u57fa\u672c\u548c\u9ed8\u8ba4\u6a21\u5f0f\u4e00\u6837&#xff0c;\u53ea\u662f<span class=\"token punctuation\">.<\/span>\u70b9\u53f7\u7ec8\u4e8e\u53ef\u4ee5\u5339\u914d\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u5305\u62ec\u6362\u884c\u7b26&#xff0c;\u8fd9\u65f6\u6240\u6709\u6587\u672c\u5c31\u662f\u4e00\u4e2a\u957f\u957f\u7684\u53ea\u6709\u4e00\u884c\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token operator\">^<\/span>\u5c31\u662f\u8fd9\u4e00\u884c\u5b57\u7b26\u4e32\u7684\u884c\u9996&#xff0c;$\u5c31\u662f\u8fd9\u4e00\u884c\u7684\u884c\u5c3e\u3002<\/p>\n<p>\u2464 \u591a\u884c\u6a21\u5f0f<span class=\"token punctuation\">:<\/span> \u91cd\u65b0\u5b9a\u4e49\u4e86\u884c\u7684\u6982\u5ff5&#xff0c;\u4f46\u4e0d\u5f71\u54cd<span class=\"token punctuation\">.<\/span>\u70b9\u53f7\u7684\u884c\u4e3a&#xff0c;<span class=\"token operator\">^<\/span>\u548c$\u8fd8\u662f\u884c\u9996\u884c\u5c3e\u7684\u610f\u601d&#xff0c;\u53ea\u4e0d\u8fc7\u56e0\u4e3a\u591a\u884c\u6a21\u5f0f\u53ef\u4ee5\u8bc6\u522b\u6362\u884c\u7b26\u4e86\u3002<br \/>\n<span class=\"token string\">&#034;\u5f00\u59cb&#034;<\/span>\u6307\u7684\u662f\\\\n\u540e\u7d27\u63a5\u7740\u4e0b\u4e00\u4e2a\u5b57\u7b26&#xff1b;<span class=\"token string\">&#034;\u7ed3\u675f&#034;<\/span>\u6307\u7684\u662f\\\\n\u524d\u7684\u5b57\u7b26&#xff0c;\u6ce8\u610f\u6700\u540e\u4e00\u884c\u7ed3\u5c3e\u53ef\u4ee5\u6ca1\u6709\\\\n<br \/>\n\u7b80\u5355\u8bb2&#xff0c;\u5355\u884c\u6a21\u5f0f\u53ea\u5f71\u54cd<span class=\"token punctuation\">.<\/span>\u70b9\u53f7\u884c\u4e3a&#xff0c;\u591a\u884c\u6a21\u5f0f\u91cd\u65b0\u5b9a\u4e49\u884c\u5f71\u54cd\u4e86<span class=\"token operator\">^<\/span>\u548c$<br \/>\n\u6ce8\u610f<span class=\"token punctuation\">:<\/span> \u6ce8\u610f\u5b57\u7b26\u4e32\u4e2d\u770b\u4e0d\u89c1\u7684\u6362\u884c\u7b26<span class=\"token punctuation\">,<\/span>\\\\r\\\\n\u4f1a\u5f71\u54cde$\u7684\u6d4b\u8bd5&#xff0c;e$\u53ea\u80fd\u5339\u914de\\\\n<\/p>\n<p>\u793a\u4f8b1&#xff1a;.$ \u6307\u7684\u662f\u6b64\u884c\u7684\u7ed3\u5c3e&#xff0c;\u800c\u9ed8\u8ba4\u6a21\u5f0f\u548c\u5355\u884c\u6a21\u5f0f\u90fd\u662f\u4e00\u884c&#xff0c;\u6307\u7684\u662f\u8fd9\u4e2a\u5927\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26&#xff0c;\u5c31\u662f key \u7684 y\u3002\u5982\u4e0b\u56fe\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014845-682fd3fd08dce.png\" width=\"60%\" \/> \u793a\u4f8b2&#xff1a;\u4e0a\u9762\u7b2c\u4e00\u884c happy \u4e4b\u540e&#xff0c;\u6709\u53ef\u80fd\u662f \\\\r\\\\n \u7ed3\u5c3e\u3002y$ \u5355\u884c\u5339\u914d key \u7684 y&#xff0c;\u591a\u884c\u5339\u914d happy \u548c key \u7684 y\u3002\u5982\u4e0b\u56fe\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014845-682fd3fd1e450.png\" width=\"60%\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014845-682fd3fd333da.png\" width=\"60%\" \/> \u6ce8\u610f&#xff1a; \u90fd\u662f\u9009\u62e9 \u5168\u90e8\u5339\u914d \u8fdb\u884c\u6d4b\u8bd5\u3002\u5982\u679c\u9009\u62e9 \u5339\u914d&#xff0c;\u5219\u9ed8\u8ba4\u53ea\u5339\u914d\u4e00\u6b21&#xff0c;\u5373\u4f7f\u662f\u591a\u884c\u6a21\u5f0f&#xff0c;\u4e5f\u53ea\u4f1a\u5339\u914d\u4e0a\u4e00\u4e2a y\u3002\u5982\u4e0b\u56fe\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014845-682fd3fd46cd6.png\" width=\"60%\" \/><\/p>\n<h3>2.2 \u5355\u8bcd\u5b9a\u754c\u7b26<\/h3>\n<p>\u5355\u8bcd\u5b9a\u754c\u7b26\u63cf\u8ff0\u4e00\u4e2a\u5355\u8bcd\u7684\u8fb9\u754c&#xff0c;\u5177\u4f53\u8bf4\u660e\u5982\u4e0b&#xff1a;<\/p>\n<p>\\\\b<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5355\u8bcd\u8fb9\u754c<br \/>\n\\\\B<span class=\"token punctuation\">:<\/span> \u8868\u793a\u975e\u5355\u8bcd\u8fb9\u754c<\/p>\n<p>ps<span class=\"token punctuation\">:<\/span> \u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d&#xff0c;\u5355\u8bcd\u662f\u7531<span class=\"token number\">26<\/span>\u4e2a\u5b57\u6bcd<span class=\"token punctuation\">(<\/span>\u542b\u5927\u5c0f\u5199<span class=\"token punctuation\">)<\/span>\u548c<span class=\"token number\">10<\/span>\u4e2a\u6570\u5b57\u7ec4\u6210\u7684\u4efb\u610f\u957f\u5ea6\u4e14\u8fde\u7eed\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n\u5355\u8bcd\u4e0e\u975e\u5355\u8bcd\u7c7b\u5b57\u7b26\u76f8\u90bb\u7684\u4f4d\u7f6e\u79f0\u4e3a\u5355\u8bcd\u8fb9\u754c\u3002<\/p>\n<p>\u793a\u4f8b1&#xff1a;\u4f7f\u7528 \\\\b \u5b9a\u754c\u7b26\u5339\u914d\u4e00\u4e2a\u5b8c\u6574\u7684 &#034;htm&#034; \u5355\u8bcd\u3002\u5982\u4e0b\u56fe\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014845-682fd3fd64f5f.png\" width=\"60%\" \/><\/p>\n<h3>2.3 \u5b57\u7b26\u7c7b<\/h3>\n<p>\u5b57\u7b26\u7c7b\u4e5f\u79f0\u4e3a\u5b57\u7b26\u96c6&#xff0c;\u5c31\u662f\u4e00\u4e2a\u5b57\u7b26\u5217\u8868&#xff0c;\u8868\u793a\u5339\u914d\u5b57\u7b26\u5217\u8868\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002\u4f7f\u7528\u65b9\u62ec\u53f7&#xff08;[\u2026]&#xff09;\u53ef\u4ee5\u5b9a\u4e49\u5b57\u7b26\u7c7b\u3002\u4f8b\u5982&#xff0c;[abc]&#xff0c;\u53ef\u4ee5\u5339\u914d a\u3001b\u3001c \u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u6bcd\u3002<\/p>\n<p>\u793a\u4f8b1&#xff1a;\u4e0b\u9762\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u4e86\u5339\u914d html\u3001HTML\u3001Html\u3001hTml \u6216 HTml \u7684\u5b57\u7b26\u7c7b\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014845-682fd3fd77c73.png\" width=\"60%\" \/> \u6240\u6709\u7684\u7279\u6b8a\u5b57\u7b26\u5728\u5b57\u7b26\u96c6\u4e2d\u90fd\u5931\u53bb\u4e86\u5176\u539f\u6709\u7684\u7279\u6b8a\u542b\u4e49&#xff0c;\u4ec5\u8868\u793a\u5b57\u7b26\u672c\u8eab\u3002\u5728\u5b57\u7b26\u96c6\u4e2d\u5982\u679c\u8981\u4f7f\u7528 [\u3001]\u3001- \u6216 ^&#xff0c;\u53ef\u4ee5\u5728 [\u3001]\u3001- \u6216 ^ \u5b57\u7b26\u524d\u9762\u52a0\u4e0a\u53cd\u659c\u6760&#xff0c;\u6216\u8005\u628a [\u3001] \u548c &#8211; \u653e\u5728\u5b57\u7b26\u96c6\u4e2d\u7b2c 1 \u4e2a\u5b57\u7b26\u4f4d\u7f6e&#xff0c;\u628a ^ \u653e\u5728\u975e\u7b2c 1 \u4e2a\u5b57\u7b26\u4f4d\u7f6e\u3002\u4e0b\u56fe\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914d\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014845-682fd3fd94f24.gif\" width=\"60%\" \/><\/p>\n<h3>2.4 \u9009\u62e9\u7b26<\/h3>\n<p>\u9009\u62e9\u7b26\u7c7b\u4f3c\u5b57\u7b26\u7c7b&#xff0c;\u53ef\u4ee5\u5b9e\u73b0\u9009\u62e9\u6027\u5339\u914d\u3002\u4f7f\u7528 &#034;|&#034; \u53ef\u4ee5\u5b9a\u4e49\u9009\u62e9\u5339\u914d\u6a21\u5f0f&#xff0c;\u7c7b\u4f3c Python \u8fd0\u7b97\u4e2d\u7684\u903b\u8f91\u6216\u3002&#034;|&#034; \u4ee3\u8868\u5de6\u53f3\u8868\u8fbe\u5f0f\u4efb\u610f\u5339\u914d\u4e00\u4e2a&#xff0c;\u5b83\u603b\u662f\u5148\u5c1d\u8bd5\u5339\u914d\u5de6\u4fa7\u7684\u8868\u8fbe\u5f0f&#xff0c;\u4e00\u65e6\u6210\u529f\u5339\u914d&#xff0c;\u5219\u8df3\u51fa\u5339\u914d\u53f3\u8fb9\u7684\u8868\u8fbe\u5f0f\u3002\u5982\u679c &#034;|&#034; \u6ca1\u6709\u88ab\u5305\u62ec\u5728\u5c0f\u62ec\u53f7\u4e2d&#xff0c;\u5219\u5b83\u7684\u5339\u914d\u8303\u56f4\u662f\u6574\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\u793a\u4f8b1&#xff1a;\u4e0b\u9762\u5b57\u7b26\u6a21\u5f0f\u53ef\u4ee5\u5339\u914d html&#xff0c;\u4e5f\u53ef\u4ee5\u5339\u914d Html\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014845-682fd3fdb104b.png\" width=\"60%\" \/> \u52a0\u5c0f\u62ec\u53f7&#xff0c;\u5982\u4e0b\u56fe\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014845-682fd3fdc4963.png\" width=\"60%\" \/><\/p>\n<h3>2.5 \u8303\u56f4\u7b26<\/h3>\n<p>\u4f7f\u7528\u5b57\u7b26\u7c7b\u9700\u8981\u5217\u4e3e\u6240\u6709\u53ef\u9009\u5b57\u7b26&#xff0c;\u5f53\u53ef\u9009\u5b57\u7b26\u6bd4\u8f83\u591a\u65f6\u5c31\u6bd4\u8f83\u9ebb\u70e6\u3002\u4e0d\u8fc7&#xff0c;\u5728\u5b57\u7b26\u7c7b\u4e2d\u53ef\u4ee5\u4f7f\u7528\u8fde\u5b57\u7b26 &#034;-&#034; \u5b9a\u4e49\u5b57\u7b26\u8303\u56f4\u3002\u8fde\u5b57\u7b26\u5de6\u4fa7\u5b57\u7b26\u4e3a\u8303\u56f4\u8d77\u59cb\u70b9&#xff0c;\u53f3\u4fa7\u5b57\u7b26\u4e3a\u8303\u56f4\u7ec8\u6b62\u70b9\u3002\u5b57\u7b26\u8303\u56f4\u90fd\u662f\u6839\u636e\u5b57\u7b26\u7f16\u7801\u8868\u7684\u4f4d\u7f6e\u5173\u7cfb\u6765\u786e\u5b9a\u7684\u3002\u793a\u4f8b&#xff1a;<\/p>\n<p><span class=\"token string\">&#039;[a-z]&#039;<\/span>  <span class=\"token comment\"># \u5339\u914d\u4efb\u610f\u4e00\u4e2a\u5c0f\u5199\u5b57\u6bcd<\/span><br \/>\n<span class=\"token string\">&#039;[A-Z]&#039;<\/span>  <span class=\"token comment\"># \u5339\u914d\u4efb\u610f\u4e00\u4e2a\u5927\u5199\u5b57\u6bcd<\/span><br \/>\n<span class=\"token string\">&#039;[0-9]&#039;<\/span>  <span class=\"token comment\"># \u5339\u914d\u4efb\u610f\u4e00\u4e2a\u6570\u5b57<\/span><br \/>\n<span class=\"token string\">&#039;[\\\\u4e00-\\\\u9fa5]&#039;<\/span>  <span class=\"token comment\"># \u5339\u914d\u4e2d\u6587\u5b57\u7b26<\/span><br \/>\n<span class=\"token string\">&#039;[\\\\x00-\\\\xff]&#039;<\/span>  <span class=\"token comment\"># \u5339\u914d\u5355\u5b57\u8282\u5b57\u7b26<\/span><br \/>\n<span class=\"token string\">&#039;[a-zA-Z0-9\\\\u4e00-\\\\u9fa5\\\\x00-\\\\xff]&#039;<\/span>  <span class=\"token comment\"># \u4e5f\u53ef\u4ee5\u8fde\u7740\u5199<\/span><\/p>\n<h3>2.6 \u6392\u9664\u7b26<\/h3>\n<p>\u5728\u5b57\u7b26\u7c7b\u4e2d&#xff0c;\u9664\u4e86\u8303\u56f4\u7b26\u5916&#xff0c;\u8fd8\u6709\u4e00\u4e2a\u5143\u5b57\u7b26&#xff1a;\u6392\u9664\u7b26 (^)\u3002\u5c06 &#039;^&#039; \u653e\u5230\u65b9\u62ec\u53f7\u5185\u6700\u5de6\u4fa7&#xff0c;\u8868\u793a\u6392\u9664\u5b57\u7b26\u5217\u8868&#xff0c;\u4e5f\u5c31\u662f\u5c06\u53cd\u8f6c\u8be5\u96c6\u5408\u7684\u610f\u4e49\u3002\u7c7b\u4f3c Python \u8fd0\u7b97\u4e2d\u7684\u903b\u8f91\u975e\u3002\u793a\u4f8b&#xff1a;<\/p>\n<p><span class=\"token string\">&#039;[^0-9]&#039;<\/span>  <span class=\"token comment\"># \u5339\u914d\u4efb\u610f\u4e00\u4e2a\u975e\u6570\u5b57<\/span><br \/>\n<span class=\"token string\">&#039;[^\\\\x00-\\\\xff]&#039;<\/span>  <span class=\"token comment\"># \u5339\u914d\u975e\u5355\u5b57\u8282\u5b57\u7b26<\/span><\/p>\n<h3>2.7 \u9650\u5b9a\u7b26<\/h3>\n<p>\u9650\u5b9a\u7b26\u4e5f\u79f0\u4e3a\u6570\u91cf\u8bcd&#xff0c;\u7528\u6765\u6307\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e00\u4e2a\u7ed9\u5b9a\u5b57\u7b26\u3001\u5b57\u7b26\u7c7b\u6216\u5b50\u8868\u8fbe\u5f0f\u5fc5\u987b\u8981\u51fa\u73b0\u591a\u5c11\u6b21\u624d\u80fd\u6ee1\u8db3\u5339\u914d\u3002\u5177\u4f53\u8bf4\u660e\u5982\u4e0b\u8868\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014845-682fd3fdd7d16.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>2.8 \u4efb\u610f\u5b57\u7b26<\/h3>\n<p>\u70b9\u53f7&#xff08;&#034;.&#034;&#xff09;\u80fd\u591f\u5339\u914d\u9664\u6362\u884c\u7b26 &#034;\\\\n&#034; \u4e4b\u5916\u7684\u4efb\u4f55\u5355\u5b57\u7b26\u3002\u5982\u679c\u8981\u5339\u914d\u70b9\u53f7&#xff08;&#034;.&#034;&#xff09;\u81ea\u5df1&#xff0c;\u9700\u8981\u4f7f\u7528 &#034;\\\\&#034; \u8fdb\u884c\u8f6c\u4e49\u3002\u6ce8\u610f&#xff1a; \u5728 re.DOTALL \u6a21\u5f0f\u4e0b\u4e5f\u80fd\u591f\u5339\u914d\u6362\u884c\u7b26\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;\u4f7f\u7528\u70b9\u53f7\u5143\u5b57\u7b26\u5339\u914d\u5b57\u7b26\u4e32 &#034;gooooooogle&#034; \u4e2d\u524d\u9762 6 \u4e2a\u5b57\u7b26\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014845-682fd3fdf3a37.png\" width=\"60%\" \/><\/p>\n<h3>2.9 \u8f6c\u4e49\u5b57\u7b26<\/h3>\n<p>\u8f6c\u4e49\u5b57\u7b26 &#034;\\\\&#034; \u80fd\u591f\u5c06\u7279\u6b8a\u5b57\u7b26\u53d8\u4e3a\u666e\u901a\u5b57\u7b26&#xff0c;\u5982 &#034;.\/*\/^\/$&#034; \u7b49&#xff0c;\u5176\u529f\u80fd\u4e0e Python \u5b57\u7b26\u4e32\u4e2d\u7684\u8f6c\u4e49\u5b57\u7b26\u7c7b\u4f3c\u3002\u5982\u679c\u628a\u7279\u6b8a\u5b57\u7b26\u653e\u5728\u4e2d\u62ec\u53f7\u5185\u5b9a\u4e49\u5b57\u7b26\u96c6&#xff0c;\u4e5f\u80fd\u591f\u628a\u7279\u6b8a\u5b57\u7b26\u53d8\u6210\u666e\u901a\u5b57\u7b26&#xff0c;\u5982 [*] \u7b49\u6548\u4e8e \\\\*&#xff0c;\u90fd\u53ef\u4ee5\u7528\u6765\u5339\u914d\u5b57\u7b26 *\u3002<\/p>\n<h3>2.10 \u53cd\u659c\u6760<\/h3>\n<p>\u53cd\u659c\u6760\u5b57\u7b26 &#034;\\\\&#034; \u9664\u4e86\u80fd\u591f\u8f6c\u4e49\u4e4b\u5916&#xff0c;\u8fd8\u5177\u6709\u5176\u4ed6\u529f\u80fd&#xff0c;\u5177\u4f53\u8bf4\u660e\u5982\u4e0b&#xff1a;<\/p>\n<li>\u5b9a\u4e49\u975e\u6253\u5370\u5b57\u7b26&#xff0c;\u5177\u4f53\u8bf4\u660e\u5982\u4e0b\u8868\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014846-682fd3fe12a5e.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/li>\n<li>\u9884\u5b9a\u4e49\u5b57\u7b26\u96c6&#xff0c;\u5177\u4f53\u8bf4\u660e\u5982\u4e0b\u8868\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014846-682fd3fe2e8c4.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/li>\n<li>\u5b9a\u4e49\u65ad\u8a00\u7684\u9650\u5b9a\u7b26&#xff0c;\u5177\u4f53\u8bf4\u660e\u5982\u4e0b\u8868\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014846-682fd3fe477fd.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/li>\n<h3>2.11 \u5c0f\u62ec\u53f7<\/h3>\n<p>\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d&#xff0c;\u5c0f\u62ec\u53f7\u6709\u4e24\u4e2a\u4f5c\u7528&#xff0c;\u7b80\u5355\u8bf4\u660e\u5982\u4e0b\u3002<\/p>\n<h4>2.11.1 \u5b9a\u4e49\u72ec\u7acb\u5355\u5143<\/h4>\n<p>\u5c0f\u62ec\u53f7\u53ef\u4ee5\u6539\u53d8\u9009\u62e9\u7b26\u548c\u9650\u5b9a\u7b26\u7684\u4f5c\u7528\u8303\u56f4\u3002\u793a\u4f8b&#xff1a;<\/p>\n<p><span class=\"token string\">&#039;(h|H)html&#039;<\/span><br \/>\n<span class=\"token string\">&#039;(goo){1,3}&#039;<\/span><\/p>\n<p>\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d&#xff0c;\u7b2c 1 \u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u9009\u62e9\u7b26\u8303\u56f4\u4e3a\u4e24\u4e2a\u5b57\u7b26&#xff0c;\u800c\u4e0d\u662f\u6574\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f&#xff1b;\u7b2c 2 \u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u9650\u5b9a\u7b26\u9650\u5b9a\u7684\u662f 3 \u4e2a\u5b57\u7b26&#xff0c;\u800c\u4e0d\u4ec5\u4ec5\u662f\u5de6\u4fa7\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<h4>2.11.2 \u5206\u7ec4<\/h4>\n<p>\u5c0f\u62ec\u53f7\u7684\u7b2c 2 \u4e2a\u4f5c\u7528\u5c31\u662f\u5206\u7ec4&#xff0c;\u5373\u5b9a\u4e49\u5b50\u8868\u8fbe\u5f0f&#xff0c;\u5b50\u8868\u8fbe\u5f0f\u76f8\u5f53\u4e8e\u4e00\u4e2a\u72ec\u7acb\u7684\u6b63\u5219\u8868\u8fbe\u5f0f&#xff0c;\u540e\u9762\u8981\u5b66\u5230\u7684\u53cd\u5411\u5f15\u7528\u4e0e\u5b50\u8868\u8fbe\u5f0f\u6709\u76f4\u63a5\u7684\u5173\u7cfb\u3002\u5b50\u8868\u8fbe\u5f0f\u80fd\u591f\u4e34\u65f6\u5b58\u50a8\u5176\u5339\u914d\u7684\u5b57\u7b26&#xff0c;\u7136\u540e\u53ef\u4ee5\u5728\u540e\u9762\u8fdb\u884c\u5f15\u7528\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u5141\u8bb8\u591a\u6b21\u5206\u7ec4\u3001\u5d4c\u5957\u5206\u7ec4&#xff0c;\u4ece\u8868\u8fbe\u5f0f\u5de6\u8fb9\u5f00\u59cb&#xff0c;\u7b2c\u4e00\u4e2a\u5de6\u62ec\u53f7 &#034;(&#034; \u7684\u7f16\u53f7\u4e3a1&#xff0c;\u7136\u540e\u6bcf\u9047\u5230\u4e00\u4e2a\u5206\u7ec4\u7684\u5de6\u62ec\u53f7 &#034;(&#034;&#xff0c;\u7f16\u53f7\u5c31\u52a01\u3002\u4f8b\u5982&#xff1a;<\/p>\n<p><span class=\"token string\">&#039;(a(b(c)))&#039;<\/span><\/p>\n<p>\u4e0a\u9762\u8868\u8fbe\u5f0f\u4e2d&#xff0c;\u7f16\u53f71\u7684\u5b50\u8868\u8fbe\u5f0f\u4e3aabc&#xff0c;\u7f16\u53f72\u7684\u5b50\u8868\u8fbe\u5f0f\u4e3abc&#xff0c;\u7f16\u53f73\u7684\u5b50\u8868\u8fbe\u5f0f\u4e3ac\u3002\u9664\u4e86\u9ed8\u8ba4\u7684\u7f16\u53f7\u5916&#xff0c;\u4e5f\u53ef\u4ee5\u4e3a\u5206\u7ec4\u5b9a\u4e49\u4e00\u4e2a\u522b\u540d\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token punctuation\">(<\/span>?P<span class=\"token operator\">&lt;<\/span>name<span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u4f8b\u5982\u4e0b\u9762\u7684\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914d\u5b57\u7b26\u4e32 abcabcabc<\/span><br \/>\n<span class=\"token punctuation\">(<\/span>?P<span class=\"token operator\">&lt;<\/span><span class=\"token builtin\">id<\/span><span class=\"token operator\">&gt;<\/span>abc<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<h3>2.12 \u53cd\u5411\u5f15\u7528<\/h3>\n<p>\u5728\u6b63\u5219\u8868\u8fbe\u5f0f&#xff0c;\u5982\u679c\u9047\u5230\u5206\u7ec4&#xff0c;\u5c06\u5bfc\u81f4\u5b50\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u5b57\u7b26\u88ab\u5b58\u50a8\u5230\u4e00\u4e2a\u4e34\u65f6\u7f13\u51b2\u533a\u4e2d&#xff0c;\u6240\u6355\u83b7\u7684\u6bcf\u4e2a\u5b50\u5339\u914d\u90fd\u6309\u7167\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4ece\u5de6\u81f3\u53f3\u7684\u987a\u5e8f\u8fdb\u884c\u7f16\u53f7&#xff0c;\u4ece 1 \u5f00\u59cb&#xff0c;\u8fde\u7eed\u7f16\u53f7\u76f4\u81f3\u6700\u5927 99 \u4e2a\u5b50\u8868\u8fbe\u5f0f\u3002\u6bcf\u4e2a\u7f13\u51b2\u533a\u90fd\u53ef\u4ee5\u4f7f\u7528 &#034;\\\\n&#034; \u8bbf\u95ee&#xff0c;\u5176\u4e2d n \u4e3a\u4e00\u4e2a\u6807\u8bc6\u7279\u5b9a\u7f13\u51b2\u533a\u7684\u7f16\u53f7\u3002\u793a\u4f8b&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014846-682fd3fe5f03d.png\" width=\"60%\" \/> \u5bf9\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f &#034;([ab])\\\\1&#034;&#xff0c;\u5b50\u8868\u8fbe\u5f0f &#034;[ab]&#034;&#xff0c;\u867d\u7136\u53ef\u4ee5\u5339\u914d &#039;a&#039; \u6216\u8005 &#039;b&#039;&#xff0c;\u4f46\u662f\u6355\u83b7\u7ec4\u4e00\u65e6\u5339\u914d\u6210\u529f&#xff0c;\u53cd\u5411\u5f15\u7528\u7684\u5185\u5bb9\u4e5f\u5c31\u786e\u5b9a\u4e86\u3002\u5982\u679c\u6355\u83b7\u7ec4\u5339\u914d\u5230 &#039;a&#039;&#xff0c;\u90a3\u4e48\u53cd\u5411\u5f15\u7528\u4e5f\u53ea\u80fd\u5339\u914d &#039;a&#039;&#xff0c;\u540c\u7406&#xff0c;\u5982\u679c\u6355\u83b7\u7ec4\u5339\u914d\u5230 &#039;b&#039;&#xff0c;\u90a3\u4e48\u53cd\u5411\u5f15\u7528\u4e5f\u53ea\u80fd\u5339\u914d &#039;b&#039;\u3002\u7531\u4e8e\u540e\u9762\u53cd\u5411\u5f15\u7528 &#039;\\\\1&#039; \u7684\u9650\u5236&#xff0c;\u8981\u6c42\u5fc5\u987b\u662f\u4e24\u4e2a\u76f8\u540c\u7684\u5b57\u7b26&#xff0c;\u5728\u8fd9\u91cc\u4e5f\u5c31\u662f &#039;aa&#039; \u6216\u8005 &#039;bb&#039; \u624d\u80fd\u5339\u914d\u6210\u529f\u3002<\/p>\n<h3>2.13 \u7279\u6b8a\u6784\u9020<\/h3>\n<p>\u5c0f\u62ec\u53f7\u4e0d\u4ec5\u53ef\u4ee5\u5206\u7ec4&#xff0c;\u4e5f\u53ef\u4ee5\u6784\u9020\u7279\u6b8a\u7684\u7ed3\u6784&#xff0c;\u5177\u4f53\u8bf4\u660e\u5982\u4e0b&#xff1a;<\/p>\n<li>\n<p>\u4f7f\u7528\u4e0b\u9762\u8bed\u6cd5\u53ef\u4ee5\u8bbe\u8ba1\u5c0f\u62ec\u53f7\u4e0d\u5206\u7ec4&#xff0c;\u4ec5\u4f5c\u4e3a\u72ec\u7acb\u5355\u5143\u7528\u4e8e &#039;|&#039; \u6216\u91cd\u590d\u5339\u914d\u3002<\/p>\n<p> <span class=\"token punctuation\">(<\/span>?<span class=\"token punctuation\">:<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u4f8b\u5982&#xff0c;\u4e0b\u9762\u8868\u8fbe\u5f0f\u4ec5\u7528\u4e8e\u754c\u5b9a\u903b\u8f91\u4f5c\u7528\u8303\u56f4&#xff0c;\u4e0d\u7528\u6765\u5206\u7ec4<\/span><br \/>\n<span class=\"token punctuation\">(<\/span>?<span class=\"token punctuation\">:<\/span>\\\\w<span class=\"token punctuation\">)<\/span><span class=\"token operator\">*<\/span>  <span class=\"token comment\"># \u5339\u914d0\u4e2a\u6216\u591a\u4e2a\u5355\u8bcd\u5b57\u7b26<\/span><br \/>\n<span class=\"token punctuation\">(<\/span>?<span class=\"token punctuation\">:<\/span>html<span class=\"token operator\">|<\/span>htm<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914dhtml&#xff0c;\u6216\u8005\u5339\u914dhtm<\/span>\n <\/li>\n<li>\n<p>\u5b9a\u4e49\u5339\u914d\u6a21\u5f0f&#xff0c;\u4f7f\u7528\u4e0b\u9762\u8bed\u6cd5\u53ef\u4ee5\u5b9a\u4e49\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u6a21\u5f0f\u3002<\/p>\n<p> <span class=\"token punctuation\">(<\/span>?aiLmsux<span class=\"token punctuation\">)<\/span>\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32<br \/>\naiLmsux \u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4ee3\u8868\u4e00\u79cd\u5339\u914d\u6a21\u5f0f&#xff0c;\u5177\u4f53\u8bf4\u660e\u53c2\u8003 <span class=\"token number\">2.14<\/span> \u5339\u914d\u6a21\u5f0f \u5c0f\u8282\u4ecb\u7ecd<br \/>\n<span class=\"token punctuation\">(<\/span>?aiLmsux<span class=\"token punctuation\">)<\/span>\u53ea\u80fd\u591f\u7528\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5f00\u5934&#xff0c;\u53ef\u4ee5\u591a\u9009\u3002\u4f8b\u5982&#xff0c;\u4e0b\u9762\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914da&#xff0c;\u4e5f\u53ef\u4ee5\u5339\u914dA<span class=\"token punctuation\">.<\/span><br \/>\n<span class=\"token punctuation\">(<\/span>?i<span class=\"token punctuation\">)<\/span>a\n <\/li>\n<li>\n<p>\u6ce8\u91ca&#xff1a;\u4f7f\u7528\u4e0b\u9762\u8bed\u6cd5\u53ef\u4ee5\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u6dfb\u52a0\u6ce8\u91ca\u4fe1\u606f&#xff0c;&#034;#&#034; \u540e\u9762\u7684\u6587\u672c\u4f5c\u4e3a\u6ce8\u91ca\u5185\u5bb9\u5c06\u88ab\u5ffd\u7565\u6389\u3002<\/p>\n<p> <span class=\"token punctuation\">(<\/span>?<span class=\"token comment\">#\u6ce8\u91ca\u4fe1\u606f)<\/span><br \/>\n\u4f8b\u5982&#xff0c;\u5728\u4e0b\u9762\u8868\u8fbe\u5f0f\u4e2d\u6dfb\u52a0\u4e00\u53e5\u6ce8\u91ca&#xff0c;\u4ee5\u4fbf\u8868\u8fbe\u5f0f\u9605\u8bfb\u548c\u7ef4\u62a4<br \/>\na<span class=\"token punctuation\">(<\/span>?<span class=\"token comment\">#\u5339\u914d\u5b57\u7b26 abc)bc<\/span><br \/>\n\u4e0a\u9762\u8868\u8fbe\u5f0f\u4ec5\u5339\u914d\u5b57\u7b26\u4e32abc&#xff0c;\u5c0f\u62ec\u53f7\u5185\u7684\u5185\u5bb9\u5c06\u88ab\u5ffd\u7565\n <\/li>\n<li>\n<p>\u6b63\u524d\u77bb&#xff0c;\u4f7f\u7528\u4e0b\u9762\u8bed\u6cd5\u53ef\u4ee5\u5b9a\u4e49\u8868\u8fbe\u5f0f\u540e\u9762\u5fc5\u987b\u6ee1\u8db3\u7279\u5b9a\u7684\u5339\u914d\u6761\u4ef6\u3002<\/p>\n<p> <span class=\"token punctuation\">(<\/span>?<span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u4f8b\u5982&#xff0c;\u4e0b\u9762\u8868\u8fbe\u5f0f\u4ec5\u5339\u914d\u540e\u9762\u5305\u542b\u6570\u5b57\u7684\u5b57\u6bcda<\/span><br \/>\na<span class=\"token punctuation\">(<\/span>?<span class=\"token operator\">&#061;<\/span>\\\\d<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u540e\u5411\u5339\u914d\u4ec5\u4f5c\u4e3a\u4e00\u4e2a\u9650\u5b9a\u6761\u4ef6&#xff0c;\u5176\u5339\u914d\u7684\u5185\u5bb9\u4e0d\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u7ed3\u679c\u3002<\/span>\n <\/li>\n<li>\n<p>\u8d1f\u524d\u77bb&#xff1a;\u4f7f\u7528\u4e0b\u9762\u8bed\u6cd5\u53ef\u4ee5\u5b9a\u4e49\u8868\u8fbe\u5f0f\u540e\u9762\u5fc5\u987b\u4e0d\u6ee1\u8db3\u7279\u5b9a\u7684\u5339\u914d\u6761\u4ef6\u3002<\/p>\n<p> <span class=\"token punctuation\">(<\/span>?!<span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u4f8b\u5982&#xff0c;\u4e0b\u9762\u8868\u8fbe\u5f0f\u4ec5\u5339\u914d\u540e\u9762\u4e0d\u5305\u542b\u6570\u5b57\u7684\u5b57\u6bcda<\/span><br \/>\na<span class=\"token punctuation\">(<\/span>?!\\\\d<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u540e\u5411\u4e0d\u5339\u914d\u4ec5\u4f5c\u4e3a\u4e00\u4e2a\u9650\u5b9a\u6761\u4ef6&#xff0c;\u5176\u5339\u914d\u7684\u5185\u5bb9\u4e0d\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u7ed3\u679c\u3002<\/span>\n <\/li>\n<li>\n<p>\u6b63\u56de\u987e&#xff1a;\u4f7f\u7528\u4e0b\u9762\u8bed\u6cd5\u53ef\u4ee5\u5b9a\u4e49\u8868\u8fbe\u5f0f\u524d\u9762\u5fc5\u987b\u6ee1\u8db3\u7279\u5b9a\u7684\u5339\u914d\u6761\u4ef6\u3002<\/p>\n<p> <span class=\"token punctuation\">(<\/span>?<span class=\"token operator\">&lt;&#061;<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u4f8b\u5982&#xff0c;\u4e0b\u9762\u8868\u8fbe\u5f0f\u4ec5\u5339\u914d\u524d\u9762\u5305\u542b\u6570\u5b57\u7684\u5b57\u6bcda<\/span><br \/>\n<span class=\"token punctuation\">(<\/span>?<span class=\"token operator\">&lt;&#061;<\/span>\\\\d<span class=\"token punctuation\">)<\/span>a  <span class=\"token comment\"># \u524d\u5411\u5339\u914d\u4ec5\u4f5c\u4e3a\u4e00\u4e2a\u9650\u5b9a\u6761\u4ef6&#xff0c;\u5176\u5339\u914d\u7684\u5185\u5bb9\u4e0d\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u7ed3\u679c\u3002<\/span>\n <\/li>\n<li>\n<p>\u8d1f\u56de\u987e&#xff1a;\u4f7f\u7528\u4e0b\u9762\u8bed\u6cd5\u53ef\u4ee5\u5b9a\u4e49\u8868\u8fbe\u5f0f\u524d\u9762\u5fc5\u987b\u4e0d\u6ee1\u8db3\u7279\u5b9a\u7684\u5339\u914d\u6761\u4ef6\u3002<\/p>\n<p> <span class=\"token punctuation\">(<\/span>?<span class=\"token operator\">&lt;<\/span>!<span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u4f8b\u5982&#xff0c;\u4e0b\u9762\u8868\u8fbe\u5f0f\u4ec5\u5339\u914d\u524d\u9762\u4e0d\u5305\u542b\u6570\u5b57\u7684\u5b57\u6bcda<\/span><br \/>\n<span class=\"token punctuation\">(<\/span>?<span class=\"token operator\">&lt;<\/span>!\\\\d<span class=\"token punctuation\">)<\/span>a  <span class=\"token comment\"># \u524d\u5411\u4e0d\u5339\u914d\u4ec5\u4f5c\u4e3a\u4e00\u4e2a\u9650\u5b9a\u6761\u4ef6&#xff0c;\u5176\u5339\u914d\u7684\u5185\u5bb9\u4e0d\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u7ed3\u679c\u3002<\/span>\n <\/li>\n<li>\n<p>\u6761\u4ef6\u5339\u914d&#xff1a;\u4f7f\u7528\u4e0b\u9762\u8bed\u6cd5\u53ef\u4ee5\u5b9a\u4e49\u6761\u4ef6\u5339\u914d\u8868\u8fbe\u5f0f\u3002<\/p>\n<p> <span class=\"token punctuation\">(<\/span>?<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">id<\/span><span class=\"token operator\">\/<\/span>name<span class=\"token punctuation\">)<\/span>yes<span class=\"token operator\">&#8211;<\/span>pattern<span class=\"token operator\">|<\/span>no<span class=\"token operator\">&#8211;<\/span>pattern<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token builtin\">id<\/span><span class=\"token punctuation\">:<\/span> \u8868\u793a\u5206\u7ec4\u7f16\u53f7<br \/>\nname<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5206\u7ec4\u7684\u522b\u540d&#xff0c;\u5982\u679c\u5bf9\u5e94\u7684\u5206\u7ec4\u5339\u914d\u5230\u5b57\u7b26&#xff0c;\u5219\u9009\u62e9 yes<span class=\"token operator\">&#8211;<\/span>pattern\u5b50\u8868\u8fbe\u5f0f\u6267\u884c\u5339\u914d<br \/>\n\u5982\u679c\u5bf9\u5e94\u7684\u5206\u7ec4\u6ca1\u6709\u5339\u914d\u5b57\u7b26&#xff0c;\u5219\u9009\u62e9 no<span class=\"token operator\">&#8211;<\/span>pattern \u5b50\u8868\u8fbe\u5f0f\u6267\u884c\u5339\u914d<br \/>\n<span class=\"token operator\">|<\/span>no<span class=\"token operator\">&#8211;<\/span>pattern<span class=\"token punctuation\">:<\/span> \u53ef\u4ee5\u7701\u7565&#xff0c;\u76f4\u63a5\u5199\u6210\u8bed\u6cd5<span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">(<\/span>?<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">id<\/span><span class=\"token operator\">\/<\/span>name<span class=\"token punctuation\">)<\/span>yes<span class=\"token operator\">&#8211;<\/span>pattern<span class=\"token punctuation\">)<\/span>\n <\/li>\n<h3>2.14 \u5339\u914d\u6a21\u5f0f<\/h3>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5305\u542b\u4e00\u4e9b\u53ef\u9009\u7684\u6807\u5fd7\u4fee\u9970\u7b26&#xff0c;\u7528\u6765\u63a7\u5236\u5339\u914d\u7684\u6a21\u5f0f\u3002\u4fee\u9970\u7b26\u4e3b\u8981\u7528\u6765\u8c03\u6574\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u89e3\u91ca&#xff0c;\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5339\u914d\u3001\u66ff\u6362\u7b49\u64cd\u4f5c\u65f6\u7684\u67d0\u4e9b\u529f\u80fd&#xff0c;\u589e\u5f3a\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u80fd\u529b\u3002\u4e0d\u540c\u7684\u8bed\u8a00\u90fd\u6709\u81ea\u5df1\u7684\u6a21\u5f0f\u8bbe\u7f6e&#xff0c;Python \u4e2d\u7684\u4e3b\u8981\u6a21\u5f0f\u4fee\u9970\u7b26\u8bf4\u660e\u5982\u4e0b\u8868\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014846-682fd3fe7216e.png\" width=\"80%\" \/> \u8fd9\u4e9b\u6807\u5fd7\u4fee\u9970\u7b26\u4e3b\u8981\u7528\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u51fd\u6570\u7684 flag \u53c2\u6570\u4e2d&#xff0c;\u4e3a\u53ef\u9009\u53c2\u6570\u3002\u591a\u4e2a\u6807\u5fd7\u53ef\u4ee5\u901a\u8fc7\u6309\u4f4d OR(|) \u6765\u6307\u5b9a&#xff0c;\u5982 re.I|re.M&#xff0c;\u88ab\u8bbe\u7f6e\u6210 I \u548c M \u6807\u5fd7\u3002<\/p>\n<h2>\u4e09\u3001re\u6a21\u5757<\/h2>\n<p>Python \u63d0\u4f9b\u4e86 re \u6a21\u5757\u7528\u4e8e\u5b9e\u73b0\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u64cd\u4f5c\u3002\u5728\u5b9e\u73b0\u65f6&#xff0c;\u53ef\u4ee5\u4f7f\u7528 re \u6a21\u5757\u63d0\u4f9b\u7684\u65b9\u6cd5&#xff0c;\u5982 search()\u3001match()\u3001findall() \u7b49&#xff0c;\u8fdb\u884c\u5b57\u7b26\u4e32\u5904\u7406&#xff0c;\u4e5f\u53ef\u4ee5\u5148\u4f7f\u7528 re \u6a21\u5757\u7684 compile() \u65b9\u6cd5\u5c06\u6a21\u5f0f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61&#xff0c;\u7136\u540e\u518d\u4f7f\u7528\u8be5\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u7684\u76f8\u5173\u65b9\u6cd5\u6765\u64cd\u4f5c\u5b57\u7b26\u4e32\u3002re \u6a21\u5757\u5728\u4f7f\u7528\u65f6&#xff0c;\u9700\u8981\u5148\u7528 import \u8bed\u53e5\u5f15\u5165&#xff0c;\u5177\u4f53\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> re<\/p>\n<h3>3.1 compile()\u65b9\u6cd5\u2014\u2014\u7f16\u8bd1\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61<\/h3>\n<p>compile() \u65b9\u6cd5\u7528\u4e8e\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u6a21\u5f0f\u3002\u7136\u540e\u53ef\u4ee5\u901a\u8fc7 RegexObject \u5bf9\u8c61\u8c03\u7528 match() \u548c search() \u7b49\u65b9\u6cd5\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">7<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span>?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> Compile a regular expression pattern<span class=\"token punctuation\">,<\/span> returning a Pattern <span class=\"token builtin\">object<\/span><span class=\"token punctuation\">.<\/span><br \/>\nFile<span class=\"token punctuation\">:<\/span>      f<span class=\"token punctuation\">:<\/span>\\\\dev_tools\\\\python\\\\python310\\\\lib\\\\re<span class=\"token punctuation\">.<\/span>py<br \/>\nType<span class=\"token punctuation\">:<\/span>      function<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>pattern<span class=\"token punctuation\">:<\/span> \u7f16\u8bd1\u65f6\u7528\u7684\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">2.<\/span>flags<span class=\"token punctuation\">:<\/span> \u7f16\u8bd1\u6807\u5fd7\u4f4d&#xff0c;\u7528\u4e8e\u4fee\u6539\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u65b9\u5f0f&#xff0c;\u5982\u662f\u5426\u533a\u5206\u5927\u5c0f\u5199\u3001\u591a\u884c\u5339\u914d\u7b49\u3002<br \/>\n<span class=\"token number\">3<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u67e5\u627e\u5305\u542b\u6307\u5b9a\u5b57\u6bcd\u7684\u5355\u8bcd\u3002\u4f7f\u7528compile()\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u53ef\u4ee5\u67e5\u627e\u5305\u542b\u6307\u5b9a\u5b57\u6bcd\u5bf9\u5e94\u5355\u8bcd\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61,\u5e76\u8f93\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u82f1\u6587\u5355\u8bcd,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p><span class=\"token comment\"># \u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;Tina is a good girl, she is cool, clever, and so on&#8230;&#034;<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">r&#039;\\\\w*oo\\\\w*&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u521b\u5efa\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>findall<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u6240\u6709\u5305\u542b&#034;oo&#034;\u7684\u5355\u8bcd<\/span><\/p>\n<h3>3.2 escape()\u65b9\u6cd5\u2014\u2014\u8f6c\u4e49\u7279\u6b8a\u5b57\u7b26<\/h3>\n<p>escape() \u65b9\u6cd5\u7528\u4e8e\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u975e\u5b57\u6bcd\u6570\u5b57\u8fdb\u884c\u8f6c\u4e49\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">8<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>escape?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>escape<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> Escape special characters <span class=\"token keyword\">in<\/span> a string<span class=\"token punctuation\">.<\/span><br \/>\nFile<span class=\"token punctuation\">:<\/span>      f<span class=\"token punctuation\">:<\/span>\\\\dev_tools\\\\python\\\\python310\\\\lib\\\\re<span class=\"token punctuation\">.<\/span>py<br \/>\nType<span class=\"token punctuation\">:<\/span>      function<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>pattern<span class=\"token punctuation\">:<\/span> \u8868\u793a\u6a21\u5f0f\u5b57\u7b26\u4e32&#xff0c;\u7531\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8f6c\u6362\u800c\u6765\u3002<br \/>\n<span class=\"token number\">2<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u8f6c\u4e49\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u975e\u5b57\u6bcd\u6570\u5b57\u8fdb\u884c\u8f6c\u4e49\u3002\u4f7f\u7528escape()\u65b9\u6cd5\u5b9e\u73b0\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u975e\u5b57\u6bcd\u6570\u5b57\u8fdb\u884c\u8f6c\u4e49,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p><span class=\"token comment\"># \u9700\u8981\u8f6c\u4e49\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;abcdefghijklmnopqrstuvwxyz0123456789!#$%&amp;*&#043;-.^_&#096;|~&#039;<\/span><br \/>\n<span class=\"token comment\"># abcdefghijklmnopqrstuvwxyz0123456789!\\\\#\\\\$%\\\\&amp;\\\\*\\\\&#043;\\\\-\\\\.\\\\^_&#096;\\\\|\\\\~<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>re<span class=\"token punctuation\">.<\/span>escape<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u8f6c\u4e49\u540e\u7684\u5b57\u7b26\u4e32<\/span><\/p>\n<h3>3.3 f\u200aindall()\u65b9\u6cd5\u2014\u2014\u641c\u7d22\u6240\u6709\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\u4e32<\/h3>\n<p>findall() \u65b9\u6cd5\u7528\u4e8e\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u6240\u6709\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\u4e32&#xff0c;\u5e76\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u3002\u5982\u679c\u5339\u914d\u6210\u529f&#xff0c;\u5219\u8fd4\u56de\u5305\u542b\u5339\u914d\u7ed3\u6784\u7684\u5217\u8868&#xff0c;\u5426\u5219\u8fd4\u56de\u7a7a\u5217\u8868\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">9<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>findall?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>findall<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nReturn a <span class=\"token builtin\">list<\/span> of <span class=\"token builtin\">all<\/span> non<span class=\"token operator\">&#8211;<\/span>overlapping matches <span class=\"token keyword\">in<\/span> the string<span class=\"token punctuation\">.<\/span><\/p>\n<p>If one <span class=\"token keyword\">or<\/span> more capturing groups are present <span class=\"token keyword\">in<\/span> the pattern<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">return<\/span><br \/>\na <span class=\"token builtin\">list<\/span> of groups<span class=\"token punctuation\">;<\/span> this will be a <span class=\"token builtin\">list<\/span> of tuples <span class=\"token keyword\">if<\/span> the pattern<br \/>\nhas more than one group<span class=\"token punctuation\">.<\/span><\/p>\n<p>Empty matches are included <span class=\"token keyword\">in<\/span> the result<span class=\"token punctuation\">.<\/span><br \/>\nFile<span class=\"token punctuation\">:<\/span>      f<span class=\"token punctuation\">:<\/span>\\\\dev_tools\\\\python\\\\python310\\\\lib\\\\re<span class=\"token punctuation\">.<\/span>py<br \/>\nType<span class=\"token punctuation\">:<\/span>      function<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>pattern<span class=\"token punctuation\">:<\/span> \u8868\u793a\u6a21\u5f0f\u5b57\u7b26\u4e32&#xff0c;\u7531\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8f6c\u6362\u800c\u6765\u3002<br \/>\n<span class=\"token number\">2.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">3.<\/span>flags<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6807\u5fd7\u4f4d&#xff0c;\u7528\u4e8e\u63a7\u5236\u5339\u914d\u65b9\u5f0f&#xff0c;\u5982\u662f\u5426\u533a\u5206\u5b57\u6bcd\u5927\u5c0f\u5199\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u7b26\u5408\u8868\u8fbe\u5f0f\u5339\u914d\u7ed3\u679c\u7684\u5217\u8868\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u641c\u7d22\u4ee5\u201cmr_\u201d\u5f00\u5934\u7684\u5b57\u7b26\u4e32\u3002\u4f7f\u7528findall()\u65b9\u6cd5\u641c\u7d22\u4ee5\u201cmr_\u201d\u5f00\u5934\u7684\u5b57\u7b26\u4e32,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">r&#039;mr_\\\\w&#043;&#039;<\/span>  <span class=\"token comment\"># \u6a21\u5f0f\u5b57\u7b26\u4e32<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>findall<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u641c\u7d22\u5b57\u7b26\u4e32&#xff0c;\u4e0d\u533a\u5206\u5927\u5c0f\u5199<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7ed3\u679c [&#039;MR_SHOP&#039;, &#039;mr_shop&#039;]<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\u9879\u76ee\u540d\u79f0MR_SHOP mr_shop&#039;<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>findall<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u641c\u7d22\u5b57\u7b26\u4e32&#xff0c;\u533a\u5206\u5927\u5c0f\u5199<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7ed3\u679c [&#039;mr_shop&#039;]<\/span><\/p>\n<h3>3.4 f\u200ainditer()\u65b9\u6cd5\u2014\u2014\u627e\u5230\u6240\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32<\/h3>\n<p>finditer() \u65b9\u6cd5\u548c findall() \u65b9\u6cd5\u7c7b\u4f3c&#xff0c;\u5b83\u7684\u529f\u80fd\u4e3a\u5728\u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6240\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32&#xff0c;\u4f46\u662f\u628a\u5b83\u4eec\u4f5c\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u5668\u8fd4\u56de\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>finditer?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>finditer<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nReturn an iterator over <span class=\"token builtin\">all<\/span> non<span class=\"token operator\">&#8211;<\/span>overlapping matches <span class=\"token keyword\">in<\/span> the<br \/>\nstring<span class=\"token punctuation\">.<\/span>  For each match<span class=\"token punctuation\">,<\/span> the iterator returns a Match <span class=\"token builtin\">object<\/span><span class=\"token punctuation\">.<\/span><\/p>\n<p>Empty matches are included <span class=\"token keyword\">in<\/span> the result<span class=\"token punctuation\">.<\/span><br \/>\nFile<span class=\"token punctuation\">:<\/span>      f<span class=\"token punctuation\">:<\/span>\\\\dev_tools\\\\python\\\\python310\\\\lib\\\\re<span class=\"token punctuation\">.<\/span>py<br \/>\nType<span class=\"token punctuation\">:<\/span>      function<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>pattern<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<br \/>\n<span class=\"token number\">2.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">3.<\/span>flags<span class=\"token punctuation\">:<\/span> \u8868\u793a\u6807\u5fd7\u4f4d&#xff0c;\u7528\u6237\u63a7\u5236\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u65b9\u5f0f&#xff0c;\u5982\u662f\u5426\u533a\u5206\u5927\u5c0f\u5199\u3001\u591a\u884c\u5339\u914d\u7b49\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u7b26\u5408\u8868\u8fbe\u5f0f\u5339\u914d\u7ed3\u679c\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u3002\u4f7f\u7528finditer()\u65b9\u6cd5\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u503c,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p><span class=\"token comment\"># \u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57<\/span><br \/>\nit <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>finditer<span class=\"token punctuation\">(<\/span><span class=\"token string\">r&#034;\\\\d&#043;&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;12a32bc43jf3&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u4fbf\u5229\u83b7\u53d6\u540e\u7684\u8fed\u4ee3\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">for<\/span> match <span class=\"token keyword\">in<\/span> it<span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token builtin\">type<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> match<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># &lt;class &#039;re.Match&#039;&gt; &lt;re.Match object; span&#061;(3, 5), match&#061;&#039;32&#039;&gt;<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>group<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u6570\u5b57 12 32 43 3<\/span><\/p>\n<h3>3.5 fullmatch()\u65b9\u6cd5\u2014\u2014\u5bf9\u6574\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d<\/h3>\n<p>fullmatch() \u65b9\u6cd5\u7528\u4e8e\u5bf9\u6574\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d&#xff0c;\u5982\u679c\u6574\u4e2a\u5b57\u7b26\u4e32\u5339\u914d\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6837\u5f0f&#xff0c;\u5c31\u8fd4\u56de\u4e00\u4e2a\u76f8\u5e94\u7684\u5339\u914d\u5bf9\u8c61&#xff0c;\u5426\u5219\u5c31\u8fd4\u56de\u4e00\u4e2a None\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">11<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>fullmatch?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>fullmatch<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nTry to <span class=\"token builtin\">apply<\/span> the pattern to <span class=\"token builtin\">all<\/span> of the string<span class=\"token punctuation\">,<\/span> returning<br \/>\na Match <span class=\"token builtin\">object<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">or<\/span> <span class=\"token boolean\">None<\/span> <span class=\"token keyword\">if<\/span> no match was found<span class=\"token punctuation\">.<\/span><br \/>\nFile<span class=\"token punctuation\">:<\/span>      f<span class=\"token punctuation\">:<\/span>\\\\dev_tools\\\\python\\\\python310\\\\lib\\\\re<span class=\"token punctuation\">.<\/span>py<br \/>\nType<span class=\"token punctuation\">:<\/span>      function<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>pattern<span class=\"token punctuation\">:<\/span> \u8868\u793a\u6a21\u5f0f\u5b57\u7b26\u4e32&#xff0c;\u7531\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8f6c\u6362\u800c\u6765\u3002<br \/>\n<span class=\"token number\">2.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">3.<\/span>flags<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6807\u5fd7\u4f4d&#xff0c;\u7528\u4e8e\u63a7\u5236\u5339\u914d\u65b9\u5f0f&#xff0c;\u5982\u662f\u5426\u533a\u5206\u5b57\u6bcd\u5927\u5c0f\u5199\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u5982\u679c\u6574\u4e2a string \u5339\u914d\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6837\u5f0f&#xff0c;\u5c31\u8fd4\u56de\u4e00\u4e2a\u76f8\u5e94\u7684 Match \u5bf9\u8c61\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u5339\u914d\u6574\u4e2a\u5b57\u7b26\u4e32\u3002\u4f7f\u7528fullmatch()\u65b9\u6cd5\u5b9e\u73b0\u5339\u914d\u6574\u4e2a\u5b57\u7b26\u4e32,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>string1 <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;mr00soft&#034;<\/span>  <span class=\"token comment\"># \u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token comment\"># \u5339\u914d\u5305\u62ec\u4e0b\u5212\u7ebf\u5728\u5185\u7684\u4efb\u4f55\u5b57\u7b26&#xff0c;\u5e76\u5339\u914d\u524d\u9762\u7684\u5b50\u8868\u8fbe\u5f0f\u4e00\u6b21\u6216\u591a\u6b21<\/span><br \/>\nmatch1 <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>fullmatch<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\\\\w&#043;&#039;<\/span><span class=\"token punctuation\">,<\/span> string1<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u5355\u4e2a\u5b57\u7b26\u5339\u914d\u4efb\u610f\u6b21&#xff0c;\u8d2a\u5a6a\u5339\u914d<\/span><br \/>\nmatch2 <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>fullmatch<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;.*&#039;<\/span><span class=\"token punctuation\">,<\/span> string1<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u5339\u914d\u591a\u4e2a\u6570\u5b57<\/span><br \/>\nmatch3 <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>fullmatch<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\\\\d&#043;&#039;<\/span><span class=\"token punctuation\">,<\/span> string1<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match1<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># &lt;re.Match object; span&#061;(0, 8), match&#061;&#039;mr00soft&#039;&gt;<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match2<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># &lt;re.Match object; span&#061;(0, 8), match&#061;&#039;mr00soft&#039;&gt;<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match3<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># None<\/span><\/p>\n<h3>3.6 match()\u65b9\u6cd5\u2014\u2014\u4ece\u5b57\u7b26\u5f00\u59cb\u5904\u8fdb\u884c\u5339\u914d<\/h3>\n<p>match() \u65b9\u6cd5\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u5904\u8fdb\u884c\u5339\u914d&#xff0c;\u5982\u679c\u5728\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d\u6210\u529f&#xff0c;\u5219\u8fd4\u56de Match \u5bf9\u8c61&#xff0c;\u5426\u5219\u8fd4\u56de None\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">12<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>match?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nTry to <span class=\"token builtin\">apply<\/span> the pattern at the start of the string<span class=\"token punctuation\">,<\/span> returning<br \/>\na Match <span class=\"token builtin\">object<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">or<\/span> <span class=\"token boolean\">None<\/span> <span class=\"token keyword\">if<\/span> no match was found<span class=\"token punctuation\">.<\/span><br \/>\nFile<span class=\"token punctuation\">:<\/span>      f<span class=\"token punctuation\">:<\/span>\\\\dev_tools\\\\python\\\\python310\\\\lib\\\\re<span class=\"token punctuation\">.<\/span>py<br \/>\nType<span class=\"token punctuation\">:<\/span>      function<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>pattern<span class=\"token punctuation\">:<\/span> \u8868\u793a\u6a21\u5f0f\u5b57\u7b26\u4e32&#xff0c;\u7531\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8f6c\u6362\u800c\u6765\u3002<br \/>\n<span class=\"token number\">2.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">3.<\/span>flags<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6807\u5fd7\u4f4d&#xff0c;\u7528\u4e8e\u63a7\u5236\u5339\u914d\u65b9\u5f0f&#xff0c;\u5982\u662f\u5426\u533a\u5206\u5b57\u6bcd\u5927\u5c0f\u5199\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u5982\u679c string \u7684\u5f00\u59cb\u5904\u6709 <span class=\"token number\">0<\/span> \u6216\u591a\u4e2a\u5b57\u7b26\u5339\u914d\u5230\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u6837\u5f0f&#xff0c;\u5c31\u8fd4\u56de\u4e00\u4e2a\u76f8\u5e94\u7684Match \u5bf9\u8c61\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u5224\u65ad\u5339\u914d\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u201cmr_\u201d\u5f00\u5934\u3002\u4f7f\u7528match()\u65b9\u6cd5\u5224\u65ad\u5339\u914d\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u201cmr_\u201d\u5f00\u5934,\u4e0d\u533a\u5206\u5b57\u6bcd\u5927\u5c0f\u5199,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">r&#039;mr_\\\\w&#043;&#039;<\/span>  <span class=\"token comment\"># \u6a21\u5f0f\u5b57\u7b26\u4e32<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u5b57\u7b26\u4e32&#xff0c;\u4e0d\u533a\u5206\u5927\u5c0f\u5199<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7ed3\u679c &lt;re.Match object; span&#061;(0, 7), match&#061;&#039;MR_SHOP&#039;&gt;<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\u9879\u76ee\u540d\u79f0MR_SHOP mr_shop&#039;<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u5b57\u7b26\u4e32&#xff0c;\u4e0d\u533a\u5206\u5927\u5c0f\u5199<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7ed3\u679c None<\/span><\/p>\n<h3>3.7 search()\u65b9\u6cd5\u2014\u2014\u641c\u7d22\u7b2c\u4e00\u4e2a\u5339\u914d\u4f4d\u7f6e<\/h3>\n<p>search() \u65b9\u6cd5\u7528\u4e8e\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u503c&#xff0c;\u5982\u679c\u5339\u914d\u6210\u529f&#xff0c;\u5219\u8fd4\u56de Match \u5bf9\u8c61&#xff0c;\u5426\u5219\u8fd4\u56de None\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">13<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>search?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>search<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nScan through string looking <span class=\"token keyword\">for<\/span> a match to the pattern<span class=\"token punctuation\">,<\/span> returning<br \/>\na Match <span class=\"token builtin\">object<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">or<\/span> <span class=\"token boolean\">None<\/span> <span class=\"token keyword\">if<\/span> no match was found<span class=\"token punctuation\">.<\/span><br \/>\nFile<span class=\"token punctuation\">:<\/span>      f<span class=\"token punctuation\">:<\/span>\\\\dev_tools\\\\python\\\\python310\\\\lib\\\\re<span class=\"token punctuation\">.<\/span>py<br \/>\nType<span class=\"token punctuation\">:<\/span>      function<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>pattern<span class=\"token punctuation\">:<\/span> \u8868\u793a\u6a21\u5f0f\u5b57\u7b26\u4e32&#xff0c;\u7531\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8f6c\u6362\u800c\u6765\u3002<br \/>\n<span class=\"token number\">2.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">3.<\/span>flags<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6807\u5fd7\u4f4d&#xff0c;\u7528\u4e8e\u63a7\u5236\u5339\u914d\u65b9\u5f0f&#xff0c;\u5982\u662f\u5426\u533a\u5206\u5b57\u6bcd\u5927\u5c0f\u5199\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\u627e\u5230\u5339\u914d\u6837\u5f0f\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u5e76\u8fd4\u56de\u4e00\u4e2a\u76f8\u5e94\u7684\u5339\u914d\u5bf9\u8c61\u3002\u5982\u679c\u6ca1\u6709\u5339\u914d&#xff0c;\u5c31\u8fd4\u56de\u4e00\u4e2a <span class=\"token boolean\">None<\/span>\u3002<br \/>\n\u6ce8\u610f&#xff0c;\u8fd9\u548c\u627e\u5230\u4e00\u4e2a\u96f6\u957f\u5ea6\u5339\u914d\u662f\u4e0d\u540c\u7684\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u641c\u7d22\u7b2c\u4e00\u4e2a\u4ee5\u201cmr_\u201d\u5f00\u5934\u7684\u5b57\u7b26\u4e32,\u4f7f\u7528search()\u65b9\u6cd5\u641c\u7d22\u7b2c\u4e00\u4e2a\u4ee5\u201cmr_\u201d\u5f00\u5934\u7684\u5b57\u7b26\u4e32,\u4e0d\u533a\u5206\u5b57\u6bcd\u5927\u5c0f\u5199,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">r&#039;mr_\\\\w&#043;&#039;<\/span>  <span class=\"token comment\"># \u6a21\u5f0f\u5b57\u7b26\u4e32<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>search<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u641c\u7d22\u5b57\u7b26\u4e32&#xff0c;\u4e0d\u533a\u5206\u5927\u5c0f\u5199<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7ed3\u679c &lt;re.Match object; span&#061;(0, 7), match&#061;&#039;MR_SHOP&#039;&gt;<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\u9879\u76ee\u540d\u79f0MR_SHOP mr_shop&#039;<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>search<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u641c\u7d22\u5b57\u7b26\u4e32&#xff0c;\u4e0d\u533a\u5206\u5927\u5c0f\u5199<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7ed3\u679c &lt;re.Match object; span&#061;(4, 11), match&#061;&#039;MR_SHOP&#039;&gt;<\/span><\/p>\n<h3>3.8 split()\u65b9\u6cd5\u2014\u2014\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u5b57\u7b26\u4e32<\/h3>\n<p>split() \u65b9\u6cd5\u7528\u4e8e\u5b9e\u73b0\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u5b57\u7b26\u4e32&#xff0c;\u5e76\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u3002\u5176\u4f5c\u7528\u540c\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684 split() \u65b9\u6cd5\u7c7b\u4f3c&#xff0c;\u6240\u4e0d\u540c\u7684\u5c31\u662f\u5206\u5272\u5b57\u7b26\u7531\u6a21\u5f0f\u5b57\u7b26\u4e32\u6307\u5b9a\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">14<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>split?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>split<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> maxsplit<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nSplit the source string by the occurrences of the pattern<span class=\"token punctuation\">,<\/span><br \/>\nreturning a <span class=\"token builtin\">list<\/span> containing the resulting substrings<span class=\"token punctuation\">.<\/span>  If<br \/>\ncapturing parentheses are used <span class=\"token keyword\">in<\/span> pattern<span class=\"token punctuation\">,<\/span> then the text of <span class=\"token builtin\">all<\/span><br \/>\ngroups <span class=\"token keyword\">in<\/span> the pattern are also returned <span class=\"token keyword\">as<\/span> part of the resulting<br \/>\n<span class=\"token builtin\">list<\/span><span class=\"token punctuation\">.<\/span>  If maxsplit <span class=\"token keyword\">is<\/span> nonzero<span class=\"token punctuation\">,<\/span> at most maxsplit splits occur<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token keyword\">and<\/span> the remainder of the string <span class=\"token keyword\">is<\/span> returned <span class=\"token keyword\">as<\/span> the final element<br \/>\nof the <span class=\"token builtin\">list<\/span><span class=\"token punctuation\">.<\/span><br \/>\nFile<span class=\"token punctuation\">:<\/span>      f<span class=\"token punctuation\">:<\/span>\\\\dev_tools\\\\python\\\\python310\\\\lib\\\\re<span class=\"token punctuation\">.<\/span>py<br \/>\nType<span class=\"token punctuation\">:<\/span>      function<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>pattern<span class=\"token punctuation\">:<\/span> \u8868\u793a\u6a21\u5f0f\u5b57\u7b26\u4e32&#xff0c;\u7531\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8f6c\u6362\u800c\u6765\u3002<br \/>\n<span class=\"token number\">2.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">3.<\/span>maxsplit<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6700\u5927\u7684\u62c6\u5206\u6b21\u6570\u3002<br \/>\n<span class=\"token number\">4.<\/span>flags<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6807\u5fd7\u4f4d&#xff0c;\u7528\u4e8e\u63a7\u5236\u5339\u914d\u65b9\u5f0f&#xff0c;\u5982\u662f\u5426\u533a\u5206\u5b57\u6bcd\u5927\u5c0f\u5199\u3002<br \/>\n<span class=\"token number\">5<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u5206\u5272\u540e\u7684\u5b57\u7b26\u4e32\u5217\u8868\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u4ece\u7ed9\u5b9a\u7684 URL \u5730\u5740\u4e2d\u63d0\u53d6\u51fa\u8bf7\u6c42\u5730\u5740\u548c\u5404\u4e2a\u53c2\u6570\u3002\u4f7f\u7528split()\u65b9\u6cd5\u5b9e\u73b0\u4ece\u7ed9\u5b9a\u7684URL\u5730\u5740\u4e2d\u63d0\u53d6\u51fa\u8bf7\u6c42\u5730\u5740\u548c\u5404\u4e2a\u53c2\u6570\u7684\u529f\u80fd,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">r&#039;[?|&amp;]&#039;<\/span>  <span class=\"token comment\"># \u5b9a\u4e49\u5206\u5272\u7b26<\/span><br \/>\nurl <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;http:\/\/www.mingrisoft.com\/login.jsp?username&#061;&#034;mr&#034;&amp;pwd&#061;&#034;mrsoft&#034;&#039;<\/span><br \/>\nresult <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>split<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> url<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u5272\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>result<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># [&#039;http:\/\/www.mingrisoft.com\/login.jsp&#039;, &#039;username&#061;&#034;mr&#034;&#039;, &#039;pwd&#061;&#034;mrsoft&#034;&#039;]<\/span><\/p>\n<h3>3.9 sub()\u65b9\u6cd5\u2014\u2014\u5b57\u7b26\u4e32\u66ff\u6362<\/h3>\n<p>sub() \u65b9\u6cd5\u7528\u4e8e\u5b9e\u73b0\u5b57\u7b26\u4e32\u66ff\u6362\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">15<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>sub?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>sub<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> repl<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> count<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nReturn the string obtained by replacing the leftmost<br \/>\nnon<span class=\"token operator\">&#8211;<\/span>overlapping occurrences of the pattern <span class=\"token keyword\">in<\/span> string by the<br \/>\nreplacement repl<span class=\"token punctuation\">.<\/span>  repl can be either a string <span class=\"token keyword\">or<\/span> a <span class=\"token builtin\">callable<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> a string<span class=\"token punctuation\">,<\/span> backslash escapes <span class=\"token keyword\">in<\/span> it are processed<span class=\"token punctuation\">.<\/span>  If it <span class=\"token keyword\">is<\/span><br \/>\na <span class=\"token builtin\">callable<\/span><span class=\"token punctuation\">,<\/span> it&#039;s passed the Match <span class=\"token builtin\">object<\/span> <span class=\"token keyword\">and<\/span> must <span class=\"token keyword\">return<\/span><br \/>\na replacement string to be used<span class=\"token punctuation\">.<\/span><br \/>\nFile<span class=\"token punctuation\">:<\/span>      f<span class=\"token punctuation\">:<\/span>\\\\dev_tools\\\\python\\\\python310\\\\lib\\\\re<span class=\"token punctuation\">.<\/span>py<br \/>\nType<span class=\"token punctuation\">:<\/span>      function<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>pattern<span class=\"token punctuation\">:<\/span> \u8868\u793a\u6a21\u5f0f\u5b57\u7b26\u4e32&#xff0c;\u7531\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8f6c\u6362\u800c\u6765\u3002<br \/>\n<span class=\"token number\">2.<\/span>repl<span class=\"token punctuation\">:<\/span> \u8868\u793a\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">3.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u8981\u88ab\u67e5\u627e\u66ff\u6362\u7684\u539f\u59cb\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">4.<\/span>count<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6a21\u5f0f\u5339\u914d\u540e\u66ff\u6362\u7684\u6700\u5927\u6b21\u6570&#xff0c;\u9ed8\u8ba4\u503c\u4e3a <span class=\"token number\">0<\/span>&#xff0c;\u8868\u793a\u66ff\u6362\u6240\u6709\u7684\u5339\u914d\u3002<br \/>\n<span class=\"token number\">5.<\/span>flags<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6807\u5fd7\u4f4d&#xff0c;\u7528\u4e8e\u63a7\u5236\u5339\u914d\u65b9\u5f0f&#xff0c;\u5982\u662f\u5426\u533a\u5206\u5b57\u6bcd\u5927\u5c0f\u5199\u3002<br \/>\n<span class=\"token number\">6<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b1:\u9690\u85cf\u4e2d\u5956\u4fe1\u606f\u4e2d\u7684\u624b\u673a\u53f7\u7801\u3002\u4f7f\u7528sub()\u65b9\u6cd5\u5b9e\u73b0\u9690\u85cf\u4e2d\u5956\u4fe1\u606f\u4e2d\u7684\u624b\u673a\u53f7\u7801\u7684\u529f\u80fd,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">r&#039;1[34578]\\\\d{9}&#039;<\/span>  <span class=\"token comment\"># \u5b9a\u4e49\u8981\u66ff\u6362\u7684\u6a21\u5f0f\u5b57\u7b26\u4e32<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\u4e2d\u5956\u53f7\u7801\u4e3a&#xff1a;84978981 \u8054\u7cfb\u7535\u8bdd\u4e3a&#xff1a;13611111111&#039;<\/span><br \/>\nresult <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>sub<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;1XXXXXXXXXX&#039;<\/span><span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u66ff\u6362\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>result<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u4e2d\u5956\u53f7\u7801\u4e3a&#xff1a;84978981 \u8054\u7cfb\u7535\u8bdd\u4e3a&#xff1a;1XXXXXXXXXX<\/span><\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b2:\u6392\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6c49\u5b57\u3001\u5b57\u6bcd\u4e0e\u6570\u5b57\u3002\u4f7f\u7528sub()\u65b9\u6cd5\u6392\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6c49\u5b57\u3001\u5b57\u6bcd\u4e0e\u6570\u5b57,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re<\/p>\n<p><span class=\"token comment\"># \u5b9a\u4e49\u9700\u8981\u7b5b\u9009\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;abcdefghijklmnopqrstuvwxyz\u6211\u7231Python0123456789!#$%&amp;*&#043;-.^_&#096;|~&#039;<\/span><br \/>\nresult <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>sub<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[A-Za-z0-9\\\\u4e00-\\\\u9fa5]&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;&#034;<\/span><span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u66ff\u6362\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>result<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u66ff\u6362\u7ed3\u679c !#$%&amp;*&#043;-.^_&#096;|~<\/span><\/p>\n<h3>3.10 subn()\u65b9\u6cd5\u2014\u2014\u5b57\u7b26\u4e32\u66ff\u6362 &#xff08;\u542b\u66ff\u6362\u6b21\u6570&#xff09;<\/h3>\n<p>subn() \u65b9\u6cd5\u4e0e sub() \u65b9\u6cd5\u76f8\u540c&#xff0c;\u90fd\u662f\u7528\u4e8e\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u66ff\u6362\u3002\u4f46 subn() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4&#xff0c;\u5176\u4e2d\u5305\u542b\u65b0\u5b57\u7b26\u4e32\u548c\u66ff\u6362\u6b21\u6570\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">16<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>subn?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span>subn<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> repl<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> count<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nReturn a <span class=\"token number\">2<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token builtin\">tuple<\/span> containing <span class=\"token punctuation\">(<\/span>new_string<span class=\"token punctuation\">,<\/span> number<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><br \/>\nnew_string <span class=\"token keyword\">is<\/span> the string obtained by replacing the leftmost<br \/>\nnon<span class=\"token operator\">&#8211;<\/span>overlapping occurrences of the pattern <span class=\"token keyword\">in<\/span> the source<br \/>\nstring by the replacement repl<span class=\"token punctuation\">.<\/span>  number <span class=\"token keyword\">is<\/span> the number of<br \/>\nsubstitutions that were made<span class=\"token punctuation\">.<\/span> repl can be either a string <span class=\"token keyword\">or<\/span> a<br \/>\n<span class=\"token builtin\">callable<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">if<\/span> a string<span class=\"token punctuation\">,<\/span> backslash escapes <span class=\"token keyword\">in<\/span> it are processed<span class=\"token punctuation\">.<\/span><br \/>\nIf it <span class=\"token keyword\">is<\/span> a <span class=\"token builtin\">callable<\/span><span class=\"token punctuation\">,<\/span> it&#039;s passed the Match <span class=\"token builtin\">object<\/span> <span class=\"token keyword\">and<\/span> must<br \/>\n<span class=\"token keyword\">return<\/span> a replacement string to be used<span class=\"token punctuation\">.<\/span><br \/>\nFile<span class=\"token punctuation\">:<\/span>      f<span class=\"token punctuation\">:<\/span>\\\\dev_tools\\\\python\\\\python310\\\\lib\\\\re<span class=\"token punctuation\">.<\/span>py<br \/>\nType<span class=\"token punctuation\">:<\/span>      function<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>pattern<span class=\"token punctuation\">:<\/span> \u8868\u793a\u6a21\u5f0f\u5b57\u7b26\u4e32&#xff0c;\u7531\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8f6c\u6362\u800c\u6765\u3002<br \/>\n<span class=\"token number\">2.<\/span>repl<span class=\"token punctuation\">:<\/span> \u8868\u793a\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">3.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u8981\u88ab\u67e5\u627e\u66ff\u6362\u7684\u539f\u59cb\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">4.<\/span>count<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6a21\u5f0f\u5339\u914d\u540e\u66ff\u6362\u7684\u6700\u5927\u6b21\u6570&#xff0c;\u9ed8\u8ba4\u503c\u4e3a <span class=\"token number\">0<\/span>&#xff0c;\u8868\u793a\u66ff\u6362\u6240\u6709\u7684\u5339\u914d\u3002<br \/>\n<span class=\"token number\">5.<\/span>flags<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6807\u5fd7\u4f4d&#xff0c;\u7528\u4e8e\u63a7\u5236\u5339\u914d\u65b9\u5f0f&#xff0c;\u5982\u662f\u5426\u533a\u5206\u5b57\u6bcd\u5927\u5c0f\u5199\u3002<br \/>\n<span class=\"token number\">6<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u66ff\u6362\u540e\u5b57\u7b26\u4e32\u4e0e\u66ff\u6362\u6b21\u6570\u7684\u5143\u7ec4\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5185\u5bb9\u3002\u4f7f\u7528subn()\u65b9\u6cd5\u5b9e\u73b0\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5185\u5bb9\u7684\u529f\u80fd,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p><span class=\"token comment\"># (&#039;AA3456abcdef&#039;, 2)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>re<span class=\"token punctuation\">.<\/span>subn<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;[1-2]&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;A&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;123456abcdef&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5143\u7ec4&#xff0c;\u5305\u542b\u65b0\u5b57\u7b26\u4e32\u4e0e\u66ff\u6362\u6b21\u6570<\/span><br \/>\n<span class=\"token comment\"># I have A, I have B ,I have C<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>re<span class=\"token punctuation\">.<\/span>sub<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;g.t&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;have&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;I get A, I got B ,I gut C&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u65b0\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token comment\"># (&#039;I have A, I have B ,I have C&#039;, 3)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>re<span class=\"token punctuation\">.<\/span>subn<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;g.t&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;have&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;I get A, I got B ,I gut C&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5143\u7ec4&#xff0c;\u5305\u542b\u65b0\u5b57\u7b26\u4e32\u4e0e\u66ff\u6362\u6b21\u6570<\/span><\/p>\n<h3>3.11 Pattern\u7684\u5c5e\u6027\u4ee5\u53ca\u65b9\u6cd5<\/h3>\n<h4>3.11.1 Pattern.flags\u5c5e\u6027\u2014\u2014\u83b7\u53d6\u6b63\u5219\u5339\u914d\u6807\u8bb0<\/h4>\n<p>Pattern.flags \u5c5e\u6027\u7528\u4e8e\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u5339\u914d\u6a21\u5f0f\u5bf9\u5e94\u7684\u6807\u8bb0\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>Pattern<span class=\"token punctuation\">.<\/span>flags<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u5e38\u89c1\u5339\u914d\u6a21\u5f0f\u5bf9\u5e94\u7684\u6807\u8bb0\u3002\u4f7f\u7528Pattern.flags\u5c5e\u6027\u83b7\u53d6\u5e38\u89c1\u5339\u914d\u6a21\u5f0f\u5bf9\u5e94\u7684\u6807\u8bb0,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern_I <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\\\\w&#043;&#039;<\/span><span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span>re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u6a21\u5f0f&#xff0c;\u5ffd\u7565\u5927\u5c0f\u5199<\/span><br \/>\npattern_M <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\\\\w&#043;&#039;<\/span><span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span>re<span class=\"token punctuation\">.<\/span>M<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u6a21\u5f0f&#xff0c;\u591a\u884c<\/span><br \/>\npattern_S <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\\\\w&#043;&#039;<\/span><span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span>re<span class=\"token punctuation\">.<\/span>S<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u6a21\u5f0f&#xff0c;\u4f7f\u7528&#034;.&#034;\u5b57\u7b26\u5339\u914d\u6240\u6709\u5b57\u7b26&#xff0c;\u5305\u62ec\u6362\u884c\u7b26<\/span><br \/>\npattern_X <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\\\\w&#043;&#039;<\/span><span class=\"token punctuation\">,<\/span> flags<span class=\"token operator\">&#061;<\/span>re<span class=\"token punctuation\">.<\/span>X<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u6a21\u5f0f&#xff0c;\u5ffd\u7565\u6a21\u5f0f\u5b57\u7b26\u4e32\u4e2d\u672a\u8f6c\u4e49\u7684\u7a7a\u683c\u548c\u6ce8\u91ca<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u5339\u914d\u6a21\u5f0fI\u7684\u6807\u8bb0\u4e3a&#xff1a;&#039;<\/span><span class=\"token punctuation\">,<\/span> pattern_I<span class=\"token punctuation\">.<\/span>flags<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u5339\u914d\u6a21\u5f0fM\u7684\u6807\u8bb0\u4e3a&#xff1a;&#039;<\/span><span class=\"token punctuation\">,<\/span> pattern_M<span class=\"token punctuation\">.<\/span>flags<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u5339\u914d\u6a21\u5f0fS\u7684\u6807\u8bb0\u4e3a&#xff1a;&#039;<\/span><span class=\"token punctuation\">,<\/span> pattern_S<span class=\"token punctuation\">.<\/span>flags<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u5339\u914d\u6a21\u5f0fX\u7684\u6807\u8bb0\u4e3a&#xff1a;&#039;<\/span><span class=\"token punctuation\">,<\/span> pattern_X<span class=\"token punctuation\">.<\/span>flags<span class=\"token punctuation\">)<\/span><\/p>\n<h4>3.11.2 Pattern.groups\u5c5e\u6027\u2014\u2014\u6355\u83b7\u7ec4\u5408\u7684\u6570\u91cf<\/h4>\n<p>Pattern.groups \u5c5e\u6027\u7528\u4e8e\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7ec4\u5408\u7684\u6570\u91cf\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>Pattern<span class=\"token punctuation\">.<\/span>groups<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b: \u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7ec4\u5408\u7684\u6570\u91cf\u3002\u4f7f\u7528Pattern.groups\u5c5e\u6027\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7ec4\u5408\u7684\u6570\u91cf,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(\\\\w&#043;) (\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u7ec4\u5408\u8868\u8fbe\u5f0f<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u7ec4\u5408\u6570\u91cf\u4e3a&#xff1a;&#039;<\/span><span class=\"token punctuation\">,<\/span> pattern<span class=\"token punctuation\">.<\/span>groups<span class=\"token punctuation\">)<\/span><\/p>\n<h4>3.11.3 Pattern.groupindex\u5c5e\u6027\u2014\u2014\u83b7\u53d6\u5206\u7ec4\u540d\u79f0\u4e0e\u5bf9\u5e94\u7684\u5206\u7ec4\u7f16\u53f7<\/h4>\n<p>Pattern.groupindex \u5c5e\u6027\u7528\u4e8e\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u5206\u7ec4\u540d\u79f0\u4e0e\u5bf9\u5e94\u7684\u5206\u7ec4\u7f16\u53f7\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>Pattern<span class=\"token punctuation\">.<\/span>groupindex<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u8868\u8fbe\u5f0f\u4e2d\u5206\u7ec4\u540d\u79f0\u4e0e\u5bf9\u5e94\u7684\u5206\u7ec4\u7f16\u53f7\u3002\u4f7f\u7528Pattern.groupindex\u5c5e\u6027\u83b7\u53d6\u8868\u8fbe\u5f0f\u4e2d\u5206\u7ec4\u540d\u79f0\u4e0e\u5bf9\u5e94\u7684\u5206\u7ec4\u7f16\u53f7,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\n<span class=\"token comment\"># \u5206\u7ec4\u540d\u79f0\u4e0e\u5bf9\u5e94\u7684\u7f16\u53f7\u4e3a&#xff1a; {&#039;group_1&#039;: 1, &#039;group_2&#039;: 2}<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u5206\u7ec4\u540d\u79f0\u4e0e\u5bf9\u5e94\u7684\u7f16\u53f7\u4e3a&#xff1a;&#039;<\/span><span class=\"token punctuation\">,<\/span> pattern<span class=\"token punctuation\">.<\/span>groupindex<span class=\"token punctuation\">)<\/span><\/p>\n<h4>3.11.4 Pattern.pattern\u5c5e\u6027\u2014\u2014\u83b7\u53d6\u6a21\u5f0f\u5b57\u7b26\u4e32<\/h4>\n<p>Pattern.pattern \u5c5e\u6027\u7528\u4e8e\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u6a21\u5f0f\u5b57\u7b26\u4e32\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>Pattern<span class=\"token punctuation\">.<\/span>pattern<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u8868\u8fbe\u5f0f\u4e2d\u7684\u6a21\u5f0f\u5b57\u7b26\u4e32\u3002\u4f7f\u7528Pattern.pattern\u5c5e\u6027\u83b7\u53d6\u8868\u8fbe\u5f0f\u4e2d\u7684\u6a21\u5f0f\u5b57\u7b26\u4e32,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">.<\/span>pattern<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># (?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)<\/span><\/p>\n<h4>3.11.5 Pattern.f\u200aindall()\u65b9\u6cd5\u2014\u2014\u641c\u7d22\u6240\u6709\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\u4e32<\/h4>\n<p>Pattern.findall() \u65b9\u6cd5\u7528\u4e8e\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u6240\u6709\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\u4e32&#xff0c;\u5e76\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u3002\u5982\u679c\u5339\u914d\u6210\u529f&#xff0c;\u5219\u8fd4\u56de\u5305\u542b\u5339\u914d\u7ed3\u6784\u7684\u5217\u8868&#xff0c;\u5426\u5219\u8fd4\u56de\u7a7a\u5217\u8868\u3002\u8be5\u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u8303\u56f4\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">21<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>findall?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>findall<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">,<\/span> pos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> endpos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">9223372036854775807<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> Return a <span class=\"token builtin\">list<\/span> of <span class=\"token builtin\">all<\/span> non<span class=\"token operator\">&#8211;<\/span>overlapping matches of pattern <span class=\"token keyword\">in<\/span> string<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_function_or_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">2.<\/span>pos<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5339\u914d\u7684\u8d77\u59cb\u4f4d\u7f6e&#xff0c;\u8be5\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570&#xff0c;\u9ed8\u8ba4\u503c\u4e3a <span class=\"token number\">0<\/span>\u3002<br \/>\n<span class=\"token number\">3.<\/span>endpos<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5339\u914d\u7684\u7ed3\u675f\u4f4d\u7f6e&#xff0c;\u8be5\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570&#xff0c;\u9ed8\u8ba4\u4e3a\u9700\u8981\u5339\u914d\u5b57\u7b26\u4e32\u7684\u957f\u5ea6&#xff0c;\u6240\u4ee5\u53ea\u6709\u4ece pos \u5230 endpos<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span> \u7684\u5b57\u7b26\u4f1a\u88ab\u5339\u914d\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u7b26\u5408\u8868\u8fbe\u5f0f\u5339\u914d\u7ed3\u679c\u7684\u5217\u8868\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u641c\u7d22\u5b57\u7b26\u4e32\u4e2d\u4ee5\u201cmr_\u201d\u5f00\u5934\u7684\u5b57\u7b26\u4e32\u3002\u4f7f\u7528Pattern.findall()\u65b9\u6cd5\u641c\u7d22\u5b57\u7b26\u4e32\u4e2d\u4ee5\u201cmr_\u201d\u5f00\u5934\u7684\u5b57\u7b26\u4e32,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;mr_\\\\w&#043;&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;mr_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> pattern<span class=\"token punctuation\">.<\/span>findall<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u641c\u7d22\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7ed3\u679c [&#039;mr_SHOP&#039;, &#039;mr_shop&#039;]<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">.<\/span>findall<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u4e0b\u68070~5\u7684\u7ed3\u679c [&#039;mr_SH&#039;]<\/span><\/p>\n<h4>3.11.6 Pattern.f\u200ainditer()\u65b9\u6cd5\u2014\u2014\u641c\u7d22\u6240\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32<\/h4>\n<p>Pattern.finditer() \u65b9\u6cd5\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6240\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32&#xff0c;\u628a\u5b83\u4eec\u4f5c\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u5668\u8fd4\u56de\u3002\u8be5\u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u8303\u56f4\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">28<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>finditer?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>finditer<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">,<\/span> pos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> endpos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">9223372036854775807<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nReturn an iterator over <span class=\"token builtin\">all<\/span> non<span class=\"token operator\">&#8211;<\/span>overlapping matches <span class=\"token keyword\">for<\/span> the RE pattern <span class=\"token keyword\">in<\/span> string<span class=\"token punctuation\">.<\/span><\/p>\n<p>For each match<span class=\"token punctuation\">,<\/span> the iterator returns a match <span class=\"token builtin\">object<\/span><span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">2.<\/span>pos<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5339\u914d\u7684\u8d77\u59cb\u4f4d\u7f6e&#xff0c;\u8be5\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570&#xff0c;\u9ed8\u8ba4\u503c\u4e3a <span class=\"token number\">0<\/span>\u3002<br \/>\n<span class=\"token number\">3.<\/span>endpos<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5339\u914d\u7684\u7ed3\u675f\u4f4d\u7f6e&#xff0c;\u8be5\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570&#xff0c;\u9ed8\u8ba4\u4e3a\u9700\u8981\u5339\u914d\u5b57\u7b26\u4e32\u7684\u957f\u5ea6&#xff0c;\u6240\u4ee5\u53ea\u6709\u4ece pos \u5230 endpos<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span> \u7684\u5b57\u7b26\u4f1a\u88ab\u5339\u914d\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u7b26\u5408\u8868\u8fbe\u5f0f\u5339\u914d\u7ed3\u679c\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u8303\u56f4\u7684\u6240\u6709\u6570\u5b57\u3002\u4f7f\u7528Pattern.finditer()\u65b9\u6cd5\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u8303\u56f4\u7684\u6240\u6709\u6570\u5b57,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\\\\d&#043;&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;12a32bc43jf3&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nit <span class=\"token operator\">&#061;<\/span> pattern<span class=\"token punctuation\">.<\/span>finditer<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">,<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">8<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u904d\u5386\u83b7\u53d6\u540e\u7684\u8fed\u4ee3\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">for<\/span> match <span class=\"token keyword\">in<\/span> it<span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>group<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u6570\u5b57<\/span><\/p>\n<h4>3.11.7 Pattern.fullmatch()\u65b9\u6cd5\u2014\u2014\u5bf9\u6574\u4e2a\u5b57\u7b26\u4e32\u5339\u914d<\/h4>\n<p>Pattern.fullmatch() \u65b9\u6cd5\u7528\u4e8e\u5bf9\u6574\u4e2a\u5b57\u7b26\u4e32\u5339\u914d\u3002\u4ece pos \u4e0b\u6807\u4f4d\u7f6e\u5f00\u59cb\u9010\u4e2a\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d\u76f4\u5230 endpos \u4e0b\u6807\u4f4d\u7f6e&#xff0c;\u5982\u679c\u5339\u914d\u6210\u529f\u5219\u8fd4\u56de\u5339\u914d\u6210\u529f\u7684 Match \u5bf9\u8c61&#xff1b;\u5982\u679c\u6ca1\u6709\u5339\u914d\u6210\u529f&#xff0c;\u5c31\u8fd4\u56deNone\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">27<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>fullmatch?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>fullmatch<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">,<\/span> pos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> endpos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">9223372036854775807<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> Matches against <span class=\"token builtin\">all<\/span> of the string<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">2.<\/span>pos<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5339\u914d\u7684\u8d77\u59cb\u4f4d\u7f6e&#xff0c;\u8be5\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570&#xff0c;\u9ed8\u8ba4\u503c\u4e3a <span class=\"token number\">0<\/span>\u3002<br \/>\n<span class=\"token number\">3.<\/span>endpos<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5339\u914d\u7684\u7ed3\u675f\u4f4d\u7f6e&#xff0c;\u8be5\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570&#xff0c;\u9ed8\u8ba4\u4e3a\u9700\u8981\u5339\u914d\u5b57\u7b26\u4e32\u7684\u957f\u5ea6&#xff0c;\u6240\u4ee5\u53ea\u6709\u4ece pos \u5230 endpos<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span> \u7684\u5b57\u7b26\u4f1a\u88ab\u5339\u914d\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u5982\u679c\u6574\u4e2a string \u5339\u914d\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6837\u5f0f&#xff0c;\u5c31\u8fd4\u56de\u4e00\u4e2a\u76f8\u5e94\u7684 Match \u5bf9\u8c61\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5f00\u59cb\u5904\u7b26\u5408\u8868\u8fbe\u5f0f\u7684\u503c\u3002\u4f7f\u7528Pattern.fullmatch()\u65b9\u6cd5\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5f00\u59cb\u5904\u7b26\u5408\u8868\u8fbe\u5f0f\u7684\u503c,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;[a-z]&#043;&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5b9a\u4e49\u5339\u914d\u5b57\u6bcd\u7684\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;abcskd123jaw123&#039;<\/span>  <span class=\"token comment\"># \u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">.<\/span>fullmatch<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7ed3\u679c&#xff0c;\u5339\u914d\u8303\u56f40~5<\/span><\/p>\n<h4>3.11.8 Pattern.match()\u65b9\u6cd5\u2014\u2014\u4ece\u5b57\u7b26\u5f00\u59cb\u5904\u5339\u914d\u5b57\u7b26\u4e32\u7684\u503c<\/h4>\n<p>Pattern.match() \u65b9\u6cd5\u7528\u4e8e\u4ece\u5b57\u7b26\u5f00\u59cb\u5904\u5339\u914d\u5b57\u7b26\u4e32\u7684\u503c&#xff0c;\u4ece pos \u4e0b\u6807\u4f4d\u7f6e\u5f00\u59cb\u9010\u4e2a\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d\u76f4\u5230 endpos \u4e0b\u6807\u4f4d\u7f6e&#xff0c;\u5982\u679c\u5339\u914d\u6210\u529f\u5219\u8fd4\u56de\u5339\u914d\u6210\u529f\u7684 Match \u5bf9\u8c61&#xff1b;\u5982\u679c\u6ca1\u6709\u5339\u914d\u6210\u529f&#xff0c;\u5c31\u8fd4\u56de None\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">26<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>match?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">,<\/span> pos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> endpos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">9223372036854775807<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> Matches zero <span class=\"token keyword\">or<\/span> more characters at the beginning of the string<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">2.<\/span>pos<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5339\u914d\u7684\u8d77\u59cb\u4f4d\u7f6e&#xff0c;\u8be5\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570&#xff0c;\u9ed8\u8ba4\u503c\u4e3a <span class=\"token number\">0<\/span>\u3002<br \/>\n<span class=\"token number\">3.<\/span>endpos<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5339\u914d\u7684\u7ed3\u675f\u4f4d\u7f6e&#xff0c;\u8be5\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570&#xff0c;\u9ed8\u8ba4\u4e3a\u9700\u8981\u5339\u914d\u5b57\u7b26\u4e32\u7684\u957f\u5ea6&#xff0c;\u6240\u4ee5\u53ea\u6709\u4ece pos \u5230 endpos<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span> \u7684\u5b57\u7b26\u4f1a\u88ab\u5339\u914d\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u5982\u679c string \u5f00\u59cb\u7684\u4f4d\u7f6e\u53ef\u4ee5\u5339\u914d\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6837\u5f0f&#xff0c;\u5c31\u8fd4\u56de\u4e00\u4e2a\u76f8\u5e94\u7684 Match \u5bf9\u8c61\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5f00\u59cb\u5904\u7b26\u5408\u8868\u8fbe\u5f0f\u7684\u503c\u3002\u4f7f\u7528Pattern.match()\u65b9\u6cd5\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5f00\u59cb\u5904\u7b26\u5408\u8868\u8fbe\u5f0f\u7684\u503c,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;[a-z]&#043;&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5b9a\u4e49\u5339\u914d\u5b57\u6bcd\u7684\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;12312abc1234546lskdj&#039;<\/span>  <span class=\"token comment\"># \u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token comment\"># &lt;re.Match object; span&#061;(5, 8), match&#061;&#039;abc&#039;&gt;<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">,<\/span> <span class=\"token number\">5<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7ed3\u679c&#xff0c;\u5339\u914d\u8303\u56f45~10<\/span><\/p>\n<h4>3.11.9 Pattern.search()\u65b9\u6cd5\u2014\u2014\u641c\u7d22\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u4f4d\u7f6e<\/h4>\n<p>Pattern.search() \u65b9\u6cd5\u7528\u4e8e\u641c\u7d22\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u4f4d\u7f6e&#xff0c;\u4ece pos \u4e0b\u6807\u4f4d\u7f6e\u5f00\u59cb\u9010\u4e2a\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d\u76f4\u5230 endpos \u4e0b\u6807\u4f4d\u7f6e&#xff0c;\u5982\u679c\u5339\u914d\u6210\u529f&#xff0c;\u5219\u8fd4\u56de\u5339\u914d\u6210\u529f\u7684 Match \u5bf9\u8c61&#xff0c;\u5426\u5219\u8fd4\u56de None\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">25<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>search?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>search<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">,<\/span> pos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> endpos<span class=\"token operator\">&#061;<\/span><span class=\"token number\">9223372036854775807<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nScan through string looking <span class=\"token keyword\">for<\/span> a match<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">and<\/span> <span class=\"token keyword\">return<\/span> a corresponding match <span class=\"token builtin\">object<\/span> instance<span class=\"token punctuation\">.<\/span><\/p>\n<p>Return <span class=\"token boolean\">None<\/span> <span class=\"token keyword\">if<\/span> no position <span class=\"token keyword\">in<\/span> the string matches<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">2.<\/span>pos<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5339\u914d\u7684\u8d77\u59cb\u4f4d\u7f6e&#xff0c;\u8be5\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570&#xff0c;\u9ed8\u8ba4\u503c\u4e3a <span class=\"token number\">0<\/span>\u3002<br \/>\n<span class=\"token number\">3.<\/span>endpos<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5339\u914d\u7684\u7ed3\u675f\u4f4d\u7f6e&#xff0c;\u8be5\u53c2\u6570\u4e3a\u53ef\u9009\u53c2\u6570&#xff0c;\u9ed8\u8ba4\u4e3a\u9700\u8981\u5339\u914d\u5b57\u7b26\u4e32\u7684\u957f\u5ea6&#xff0c;\u6240\u4ee5\u53ea\u6709\u4ece pos \u5230 endpos<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span> \u7684\u5b57\u7b26\u4f1a\u88ab\u5339\u914d\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\u627e\u5230\u5339\u914d\u6837\u5f0f\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u5e76\u8fd4\u56de\u4e00\u4e2a\u76f8\u5e94\u7684\u5339\u914d\u5bf9\u8c61\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u503c\u4e0e\u4f4d\u7f6e\u3002\u4f7f\u7528Pattern.search()\u65b9\u6cd5\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u503c\u4e0e\u4f4d\u7f6e,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;[0-9]&#043;&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5b9a\u4e49\u5339\u914d\u6570\u5b57\u7684\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;12312abc1234546lskdj&#039;<\/span>  <span class=\"token comment\"># \u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">.<\/span>search<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7ed3\u679c&#xff0c;\u5339\u914d\u8303\u56f40~10<\/span><\/p>\n<h4>3.11.10 Pattern.split()\u65b9\u6cd5\u2014\u2014\u540csplit()\u65b9\u6cd5<\/h4>\n<p>Pattern.split() \u65b9\u6cd5\u4e0e re.split() \u65b9\u6cd5\u76f8\u540c&#xff0c;\u7528\u4e8e\u5b9e\u73b0\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u5b57\u7b26\u4e32&#xff0c;\u5e76\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u3002\u5176\u4f5c\u7528\u540c\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684 split() \u65b9\u6cd5\u7c7b\u4f3c&#xff0c;\u6240\u4e0d\u540c\u7684\u662f\u5206\u5272\u5b57\u7b26\u7531\u6a21\u5f0f\u5b57\u7b26\u4e32\u6307\u5b9a\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">24<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>split?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>split<span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">,<\/span> maxsplit<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> Split string by the occurrences of pattern<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_function_or_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">2.<\/span>maxsplit<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6700\u5927\u7684\u62c6\u5206\u6b21\u6570\u3002<br \/>\n<span class=\"token number\">3<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u5206\u5272\u540e\u7684\u5b57\u7b26\u4e32\u5217\u8868\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u4ece\u7ed9\u5b9a\u7684 URL \u5730\u5740\u4e2d\u63d0\u53d6\u51fa\u8bf7\u6c42\u5730\u5740\u548c\u5404\u4e2a\u53c2\u6570\u3002\u4f7f\u7528Pattern.split()\u65b9\u6cd5\u5b9e\u73b0\u4ece\u7ed9\u5b9a\u7684URL\u5730\u5740\u4e2d\u63d0\u53d6\u51fa\u8bf7\u6c42\u5730\u5740\u548c\u5404\u4e2a\u53c2\u6570,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;[?|&amp;]&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5b9a\u4e49\u5206\u5272\u7b26<\/span><\/p>\n<p>url <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;http:\/\/www.mingrisoft.com\/login.jsp?username&#061;&#034;mr&#034;&amp;pwd&#061;&#034;mrsoft&#034;&#039;<\/span><br \/>\n<span class=\"token comment\"># [&#039;http:\/\/www.mingrisoft.com\/login.jsp&#039;, &#039;username&#061;&#034;mr&#034;&#039;, &#039;pwd&#061;&#034;mrsoft&#034;&#039;]<\/span><br \/>\nresult <span class=\"token operator\">&#061;<\/span> pattern<span class=\"token punctuation\">.<\/span>split<span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u5272\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>result<span class=\"token punctuation\">)<\/span><\/p>\n<h4>3.11.11 Pattern.sub()\u65b9\u6cd5\u2014\u2014\u5b57\u7b26\u4e32\u66ff\u6362<\/h4>\n<p>Pattern.sub() \u65b9\u6cd5\u4e0e re.sub() \u65b9\u6cd5\u76f8\u540c&#xff0c;\u7528\u4e8e\u5b9e\u73b0\u5b57\u7b26\u4e32\u66ff\u6362\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">23<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>sub?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>sub<span class=\"token punctuation\">(<\/span>repl<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> count<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> Return the string obtained by replacing the leftmost non<span class=\"token operator\">&#8211;<\/span>overlapping<br \/>\noccurrences of pattern <span class=\"token keyword\">in<\/span> string by the replacement repl<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>repl<span class=\"token punctuation\">:<\/span> \u8868\u793a\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">2.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u8981\u88ab\u67e5\u627e\u66ff\u6362\u7684\u539f\u59cb\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">3.<\/span>count<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6a21\u5f0f\u5339\u914d\u540e\u66ff\u6362\u7684\u6700\u5927\u6b21\u6570&#xff0c;\u9ed8\u8ba4\u503c\u4e3a <span class=\"token number\">0<\/span>&#xff0c;\u8868\u793a\u66ff\u6362\u6240\u6709\u7684\u5339\u914d\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u9690\u85cf\u4e2d\u5956\u4fe1\u606f\u4e2d\u7684\u624b\u673a\u53f7\u7801\u3002\u4f7f\u7528Pattern.sub()\u65b9\u6cd5\u5b9e\u73b0\u9690\u85cf\u4e2d\u5956\u4fe1\u606f\u4e2d\u7684\u624b\u673a\u53f7\u7801\u7684\u529f\u80fd,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;1[34578]\\\\d{9}&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5b9a\u4e49\u8981\u66ff\u6362\u7684\u6a21\u5f0f\u5b57\u7b26\u4e32<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\u4e2d\u5956\u53f7\u7801\u4e3a&#xff1a;84978981 \u8054\u7cfb\u7535\u8bdd\u4e3a&#xff1a;13611111111&#039;<\/span><br \/>\nresult <span class=\"token operator\">&#061;<\/span> pattern<span class=\"token punctuation\">.<\/span>sub<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;1XXXXXXXXXX&#039;<\/span><span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u66ff\u6362\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>result<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u4e2d\u5956\u53f7\u7801\u4e3a&#xff1a;84978981 \u8054\u7cfb\u7535\u8bdd\u4e3a&#xff1a;1XXXXXXXXXX<\/span><\/p>\n<h4>3.11.12 Pattern.subn()\u65b9\u6cd5\u2014\u2014\u5b57\u7b26\u4e32\u66ff\u6362&#xff08;\u542b\u66ff\u6362\u6b21\u6570&#xff09;<\/h4>\n<p>Pattern.subn() \u65b9\u6cd5\u4e0e re.subn() \u65b9\u6cd5\u76f8\u540c&#xff0c;\u90fd\u662f\u7528\u4e8e\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u66ff\u6362\u5e76\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4&#xff0c;\u5176\u4e2d\u5305\u542b\u65b0\u5b57\u7b26\u4e32\u548c\u66ff\u6362\u6b21\u6570\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">22<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>subn?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> pattern<span class=\"token punctuation\">.<\/span>subn<span class=\"token punctuation\">(<\/span>repl<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">,<\/span> count<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> Return the <span class=\"token builtin\">tuple<\/span> <span class=\"token punctuation\">(<\/span>new_string<span class=\"token punctuation\">,<\/span> number_of_subs_made<span class=\"token punctuation\">)<\/span> found by replacing the leftmost<br \/>\nnon<span class=\"token operator\">&#8211;<\/span>overlapping occurrences of pattern <span class=\"token keyword\">with<\/span> the replacement repl<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>repl<span class=\"token punctuation\">:<\/span> \u8868\u793a\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">2.<\/span>string<span class=\"token punctuation\">:<\/span> \u8868\u793a\u8981\u88ab\u67e5\u627e\u66ff\u6362\u7684\u539f\u59cb\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">3.<\/span>count<span class=\"token punctuation\">:<\/span> \u53ef\u9009\u53c2\u6570&#xff0c;\u8868\u793a\u6a21\u5f0f\u5339\u914d\u540e\u66ff\u6362\u7684\u6700\u5927\u6b21\u6570&#xff0c;\u9ed8\u8ba4\u503c\u4e3a <span class=\"token number\">0<\/span>&#xff0c;\u8868\u793a\u66ff\u6362\u6240\u6709\u7684\u5339\u914d\u3002<br \/>\n<span class=\"token number\">4<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u66ff\u6362\u540e\u5b57\u7b26\u4e32\u4e0e\u66ff\u6362\u6b21\u6570\u7684\u5143\u7ec4\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5185\u5bb9\u3002\u4f7f\u7528Pattern.subn()\u65b9\u6cd5\u5b9e\u73b0\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5185\u5bb9\u7684\u529f\u80fd,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;[1-2]&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6570\u503c\u66ff\u6362\u8868\u8fbe\u5f0f&#xff0c;\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u76841-2<\/span><br \/>\npattern2 <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;g.t&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5b57\u5e55\u66ff\u6362\u8868\u8fbe\u5f0f&#xff0c;\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684get\u3001got\u3001gut<\/span><br \/>\n<span class=\"token comment\"># \u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;123456abcdef&#039;<\/span><br \/>\nstring2 <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;I get A, I got B ,I gut C&#039;<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">.<\/span>subn<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;A&#039;<\/span><span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5143\u7ec4&#xff0c;\u5305\u542b\u65b0\u5b57\u7b26\u4e32\u4e0e\u66ff\u6362\u6b21\u6570 (&#039;AA3456abcdef&#039;, 2)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>pattern2<span class=\"token punctuation\">.<\/span>sub<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;have&#039;<\/span><span class=\"token punctuation\">,<\/span> string2<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u65b0\u5b57\u7b26\u4e32 I have A, I have B ,I have C<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>pattern2<span class=\"token punctuation\">.<\/span>subn<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;have&#039;<\/span><span class=\"token punctuation\">,<\/span> string2<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5143\u7ec4&#xff0c;\u5305\u542b\u65b0\u5b57\u7b26\u4e32\u4e0e\u66ff\u6362\u6b21\u6570 (&#039;I have A, I have B ,I have C&#039;, 3)<\/span><\/p>\n<h3>3.12 Match\u5bf9\u8c61\u5c5e\u6027\u53ca\u65b9\u6cd5<\/h3>\n<h4>3.12.1 Match.endpos\u5c5e\u6027\u2014\u2014\u7ed3\u675f\u5339\u914d\u7684\u7d22\u5f15\u4f4d\u7f6e<\/h4>\n<p>Match.endpos \u5c5e\u6027\u7528\u4e8e\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5b57\u7b26\u4e32\u7ed3\u675f\u5339\u914d\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>Match<span class=\"token punctuation\">.<\/span>endpos<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5b57\u7b26\u4e32\u7ed3\u675f\u5339\u914d\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002\u4f7f\u7528Match.endpos\u5c5e\u6027\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5b57\u7b26\u4e32\u7ed3\u675f\u5339\u914d\u7684\u7d22\u5f15\u4f4d\u7f6e,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;mr_\\\\w&#043;&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>search<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7684Match\u5bf9\u8c61 &lt;re.Match object; span&#061;(8, 15), match&#061;&#039;mr_shop&#039;&gt;<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>endpos<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7684\u7ed3\u675f\u4f4d\u7f6e 15<\/span><\/p>\n<h4>3.12.2 Match.pos\u5c5e\u6027\u2014\u2014\u5f00\u59cb\u5339\u914d\u7684\u7d22\u5f15\u4f4d\u7f6e<\/h4>\n<p>Match.pos \u5c5e\u6027\u7528\u4e8e\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5b57\u7b26\u4e32\u5f00\u59cb\u5339\u914d\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>Match<span class=\"token punctuation\">.<\/span>pos<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5b57\u7b26\u4e32\u5f00\u59cb\u5339\u914d\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002\u4f7f\u7528Match.pos\u5c5e\u6027\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5b57\u7b26\u4e32\u5f00\u59cb\u5339\u914d\u7684\u7d22\u5f15\u4f4d\u7f6e,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;mr_\\\\w&#043;&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>search<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7684Match\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>pos<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5339\u914d\u7684\u8d77\u59cb\u4f4d\u7f6e<\/span><\/p>\n<h4>3.12.3 Match.lastindex\u5c5e\u6027\u2014\u2014\u6355\u83b7\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u5339\u914d\u7684\u7d22\u5f15\u503c<\/h4>\n<p>Match.lastindex \u5c5e\u6027\u7528\u4e8e\u6355\u83b7\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u5339\u914d\u7684\u7d22\u5f15\u503c\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>Match<span class=\"token punctuation\">.<\/span>lastindex<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5339\u914d\u5230\u7684\u7ec4\u6240\u5bf9\u5e94\u7684\u7d22\u5f15\u503c\u3002\u4f7f\u7528Match.lastindex\u5c5e\u6027\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5339\u914d\u5230\u7684\u7ec4\u6240\u5bf9\u5e94\u7684\u7d22\u5f15\u503c,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>lastindex<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u6700\u540e\u4e00\u4e2a\u5339\u914d\u5230\u7684\u7ec4\u6240\u5bf9\u5e94\u7684\u7d22\u5f15\u503c 2<\/span><\/p>\n<h4>3.12.4 Match.lastgroup\u5c5e\u6027\u2014\u2014\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5339\u914d\u7ec4\u7684\u540d\u79f0<\/h4>\n<p>Match.lastgroup \u5c5e\u6027\u7528\u4e8e\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5339\u914d\u7ec4\u7684\u540d\u79f0&#xff0c;\u5982\u679c\u5339\u914d\u7684\u7ec4\u6ca1\u6709\u540d\u5b57\u6216\u8005\u6ca1\u6709\u4ea7\u751f\u5339\u914d\u7684\u60c5\u51b5\u4e0b&#xff0c;\u5219\u8fd4\u56de None\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">44<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>lastgroup?<br \/>\nType<span class=\"token punctuation\">:<\/span>        NoneType<br \/>\nString form<span class=\"token punctuation\">:<\/span> <span class=\"token boolean\">None<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span>   <span class=\"token operator\">&lt;<\/span>no docstring<span class=\"token operator\">&gt;<\/span><\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5339\u914d\u7ec4\u7684\u540d\u79f0\u3002\u4f7f\u7528Match.lastgroup\u5c5e\u6027\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5339\u914d\u7ec4\u7684\u540d\u79f0,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>lastgroup<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u6700\u540e\u4e00\u4e2a\u5339\u914d\u7ec4\u7684\u540d\u79f0<\/span><\/p>\n<h4>3.12.5 Match.re\u5c5e\u6027\u2014\u2014\u8fd4\u56de\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61<\/h4>\n<p>Match.re \u5c5e\u6027\u7528\u4e8e\u83b7\u53d6\u4ea7\u751f Match \u5b9e\u4f8b\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">43<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>re?<br \/>\nType<span class=\"token punctuation\">:<\/span>        Pattern<br \/>\nString form<span class=\"token punctuation\">:<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\\\\\\\\d&#039;<\/span><span class=\"token punctuation\">)<\/span><br \/>\nFile<span class=\"token punctuation\">:<\/span>        f<span class=\"token punctuation\">:<\/span>\\\\dev_tools\\\\python\\\\python310\\\\lib\\\\re<span class=\"token punctuation\">.<\/span>py<br \/>\nDocstring<span class=\"token punctuation\">:<\/span>   Compiled regular expression <span class=\"token builtin\">object<\/span><span class=\"token punctuation\">.<\/span><\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u4ea7\u751fMatch\u5b9e\u4f8b\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u3002\u4f7f\u7528Match.re\u5c5e\u6027\u83b7\u53d6\u4ea7\u751fMatch\u5b9e\u4f8b\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>re<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u83b7\u53d6\u7684\u8868\u8fbe\u5f0f\u5bf9\u8c61<\/span><\/p>\n<h4>3.12.6 Match.string\u5c5e\u6027\u2014\u2014\u83b7\u53d6\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/h4>\n<p>Match.string \u5c5e\u6027\u7528\u4e8e\u83b7\u53d6 Match \u5bf9\u8c61\u4e2d\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">42<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>string?<br \/>\nType<span class=\"token punctuation\">:<\/span>        <span class=\"token builtin\">str<\/span><br \/>\nString form<span class=\"token punctuation\">:<\/span> <span class=\"token number\">123456<\/span><br \/>\nLength<span class=\"token punctuation\">:<\/span>      <span class=\"token number\">6<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token builtin\">str<\/span><span class=\"token punctuation\">(<\/span><span class=\"token builtin\">object<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;&#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token builtin\">str<\/span><br \/>\n<span class=\"token builtin\">str<\/span><span class=\"token punctuation\">(<\/span>bytes_or_buffer<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">,<\/span> encoding<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">,<\/span> errors<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token builtin\">str<\/span><\/p>\n<p>Create a new string <span class=\"token builtin\">object<\/span> <span class=\"token keyword\">from<\/span> the given <span class=\"token builtin\">object<\/span><span class=\"token punctuation\">.<\/span> If encoding <span class=\"token keyword\">or<\/span><br \/>\nerrors <span class=\"token keyword\">is<\/span> specified<span class=\"token punctuation\">,<\/span> then the <span class=\"token builtin\">object<\/span> must expose a data <span class=\"token builtin\">buffer<\/span><br \/>\nthat will be decoded using the given encoding <span class=\"token keyword\">and<\/span> error handler<span class=\"token punctuation\">.<\/span><br \/>\nOtherwise<span class=\"token punctuation\">,<\/span> returns the result of <span class=\"token builtin\">object<\/span><span class=\"token punctuation\">.<\/span>__str__<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">if<\/span> defined<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">or<\/span> <span class=\"token builtin\">repr<\/span><span class=\"token punctuation\">(<\/span><span class=\"token builtin\">object<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><br \/>\nencoding defaults to sys<span class=\"token punctuation\">.<\/span>getdefaultencoding<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><br \/>\nerrors defaults to <span class=\"token string\">&#039;strict&#039;<\/span><span class=\"token punctuation\">.<\/span><\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6Match\u5bf9\u8c61\u4e2d\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002\u4f7f\u7528Match.string\u5c5e\u6027\u83b7\u53d6Match\u5bf9\u8c61\u4e2d\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370Match\u5bf9\u8c61\u4e2d\u9700\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32 MR_SHOP mr_shop<\/span><\/p>\n<h4>3.12.7 Match.__getitem__() \u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u7684\u5206\u7ec4\u4fe1\u606f<\/h4>\n<p>Match.__getitem__() \u65b9\u6cd5\u4e0e Match.group() \u65b9\u6cd5\u76f8\u4f3c&#xff0c;\u53ea\u662f __getitem__() \u65b9\u6cd5\u6bcf\u6b21\u53ea\u80fd\u83b7\u53d6\u4e00\u4e2a\u5206\u7ec4\u7684\u4fe1\u606f\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">41<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>__getitem__?<br \/>\nSignature<span class=\"token punctuation\">:<\/span>      match<span class=\"token punctuation\">.<\/span>__getitem__<span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">\/<\/span><span class=\"token punctuation\">)<\/span><br \/>\nCall signature<span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>__getitem__<span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kwargs<span class=\"token punctuation\">)<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>           method<span class=\"token operator\">&#8211;<\/span>wrapper<br \/>\nString form<span class=\"token punctuation\">:<\/span>    <span class=\"token operator\">&lt;<\/span>method<span class=\"token operator\">&#8211;<\/span>wrapper <span class=\"token string\">&#039;__getitem__&#039;<\/span> of re<span class=\"token punctuation\">.<\/span>Match <span class=\"token builtin\">object<\/span> at <span class=\"token number\">0x000001BEA4520240<\/span><span class=\"token operator\">&gt;<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span>      Return self<span class=\"token punctuation\">[<\/span>key<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span><\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>g<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5206\u7ec4\u5bf9\u5e94\u7684\u7f16\u53f7\u6216\u5206\u7ec4\u540d\u79f0\u3002<br \/>\n<span class=\"token number\">2<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u7684\u5206\u7ec4\u4fe1\u606f\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u6307\u5b9a\u5206\u7ec4\u5bf9\u5e94\u7684\u4fe1\u606f\u3002\u4f7f\u7528Match.__getitem__()\u65b9\u6cd5\u83b7\u53d6\u6307\u5b9a\u5206\u7ec4\u5bf9\u5e94\u7684\u4fe1\u606f,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>__getitem__<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u6307\u5b9a\u5206\u7ec4\u7f16\u53f7\u5bf9\u5e94\u5185\u5bb9 MR_SHOP<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>__getitem__<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;group_2&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u6307\u5b9a\u5206\u7ec4\u540d\u5bf9\u5e94\u5185\u5bb9mr_shop<\/span><\/p>\n<h4>3.12.8 Match.end()\u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u7ed3\u675f\u6807\u8bb0<\/h4>\n<p>Match.end() \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u7ed3\u675f\u6807\u8bb0\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">40<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>end?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>end<span class=\"token punctuation\">(<\/span>group<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">\/<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> Return index of the end of the substring matched by group<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_function_or_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>group<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5206\u7ec4\u540d\u6216\u8005\u5206\u7ec4\u540d\u5bf9\u5e94\u7684\u7f16\u53f7\u3002<br \/>\n<span class=\"token number\">2<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u7ed3\u675f\u6807\u8bb0\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0\u3002\u4f7f\u7528Match.start()\u65b9\u6cd5\u4e0eMatch.end()\u65b9\u6cd5\u83b7\u53d6\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> match<span class=\"token punctuation\">.<\/span>end<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5168\u90e8\u5206\u7ec4\u7684\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0 0 15<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> match<span class=\"token punctuation\">.<\/span>end<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u7f16\u53f71\u5206\u7ec4\u7684\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0 0 7<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> match<span class=\"token punctuation\">.<\/span>end<span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u7f16\u53f72\u5206\u7ec4\u7684\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0 8 15<\/span><br \/>\n<span class=\"token comment\"># \u6253\u5370\u5206\u7ec4\u540d\u7684\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;group_1&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> match<span class=\"token punctuation\">.<\/span>end<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;group_2&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># 0 15<\/span><\/p>\n<h4>3.12.9 Match.expand()\u65b9\u6cd5\u2014\u2014\u5bf9\u201c\\\\\u201d\u8fdb\u884c\u8f6c\u4e49\u66ff\u6362\u5e76\u8fd4\u56de<\/h4>\n<p>Match.expand() \u65b9\u6cd5\u7528\u4e8e\u5b9e\u73b0\u628a\u5b57\u7b26\u4e32\u4e2d\u7528\u53cd\u659c\u6760 &#034;\\\\&#034; \u8868\u793a\u7684\u6570\u5b57\u6216\u7ec4\u540d\u66ff\u6362\u4e3a\u76f8\u5e94\u5206\u7ec4\u4e2d\u7684\u503c\u7684\u529f\u80fd\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">39<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>expand?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>expand<span class=\"token punctuation\">(<\/span>template<span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> Return the string obtained by doing backslash substitution on the string template<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token keyword\">as<\/span> done by the sub<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> method<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_function_or_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>template<span class=\"token punctuation\">:<\/span> \u8868\u793a\u9700\u8981\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n<span class=\"token number\">2<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u5bf9&#034;\\\\&#034;\u8fdb\u884c\u8f6c\u4e49\u66ff\u6362\u7684\u7ed3\u679c\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u5728\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u4f4d\u7f6e\u66ff\u6362\u5bf9\u5e94\u5206\u7ec4\u4e2d\u7684\u503c\u3002\u4f7f\u7528Match.expand()\u65b9\u6cd5,\u5728\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u4f4d\u7f6e\u66ff\u6362\u5bf9\u5e94\u5206\u7ec4\u4e2d\u7684\u503c,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5206\u7ec41\u5185\u5bb9 MR_SHOP<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5206\u7ec42\u5185\u5bb9 mr_shop<\/span><br \/>\n<span class=\"token comment\"># \u7b2c\u4e00\u79cd\u66ff\u6362\u65b9\u5f0f&#xff0c;\\\\1\u3001\\\\2\u66ff\u6362\u5206\u7ec41\u548c2\u7684\u5185\u5bb9<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>expand<span class=\"token punctuation\">(<\/span><span class=\"token string\">r&#039;first_ \\\\1 \\\\2&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># first_ MR_SHOP mr_shop<\/span><br \/>\n<span class=\"token comment\"># \u7b2c\u4e8c\u79cd\u66ff\u6362\u65b9\u5f0f&#xff0c;\\\\g&lt;1&gt;\u3001\\\\g&lt;2&gt;\u66ff\u6362\u5206\u7ec41\u548c2\u7684\u5185\u5bb9<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>expand<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;second_ \\\\g&lt;1&gt; \\\\g&lt;2&gt;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># second_ MR_SHOP mr_shop<\/span><br \/>\n<span class=\"token comment\"># \u7b2c\u4e09\u79cd\u66ff\u6362\u65b9\u5f0f&#xff0c;\\\\g&lt;group_1&gt;\u3001\\\\g&lt;group_2&gt;\u66ff\u6362\u5206\u7ec41\u548c2\u7684\u5185\u5bb9<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>expand<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;third_ \\\\g&lt;group_1&gt; \\\\g&lt;group_2&gt;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># third_ MR_SHOP mr_shop<\/span><\/p>\n<h4>3.12.10 Match.groups()\u65b9\u6cd5\u2014\u2014\u4ee5\u5143\u7ec4\u5f62\u5f0f\u8fd4\u56de\u6240\u6709\u5b50\u7ec4\u4e2d\u5185\u5bb9<\/h4>\n<p>Match.groups() \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u4ee5\u5143\u7ec4\u5f62\u5f0f\u8fd4\u56de\u6240\u6709\u5b50\u7ec4\u4e2d\u5185\u5bb9\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">38<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>groups?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>groups<span class=\"token punctuation\">(<\/span>default<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">None<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nReturn a <span class=\"token builtin\">tuple<\/span> containing <span class=\"token builtin\">all<\/span> the subgroups of the match<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">.<\/span><\/p>\n<p>default<br \/>\n  Is used <span class=\"token keyword\">for<\/span> groups that did <span class=\"token keyword\">not<\/span> participate <span class=\"token keyword\">in<\/span> the match<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_function_or_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>default<span class=\"token punctuation\">:<\/span> \u7528\u4e8e\u4e0d\u53c2\u4e0e\u5339\u914d\u7684\u60c5\u51b5&#xff0c;\u9ed8\u8ba4\u4e3a <span class=\"token boolean\">None<\/span>\u3002<br \/>\n<span class=\"token number\">2<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u4ee5\u5143\u7ec4\u5f62\u5f0f\u8fd4\u56de\u6240\u6709\u5b50\u7ec4\u4e2d\u5185\u5bb9\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u6240\u6709\u5206\u7ec4\u5bf9\u5e94\u7684\u5185\u5bb9\u3002\u4f7f\u7528Match.groups()\u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u5206\u7ec4\u5bf9\u5e94\u7684\u5185\u5bb9,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>groups<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u6240\u6709\u5206\u7ec4\u5bf9\u5e94\u7684\u5185\u5bb9 (&#039;MR_SHOP&#039;, &#039;mr_shop&#039;)<\/span><\/p>\n<h4>3.12.11 Match.group()\u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u5339\u914d\u7684\u5b50\u7ec4<\/h4>\n<p>Match.group() \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6 Match \u5bf9\u8c61\u4e2d\u4e00\u4e2a\u6216\u591a\u4e2a\u5339\u914d\u7684\u5b50\u7ec4\u3002\u5982\u679c\u53ea\u6709\u4e00\u4e2a&#xff08;0~99&#xff09;\u53c2\u6570&#xff0c;\u7ed3\u679c\u5c31\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32&#xff1b;\u5982\u679c\u6709\u591a\u4e2a\u53c2\u6570&#xff0c;\u7ed3\u679c\u5c31\u662f\u4e00\u4e2a\u5143\u7ec4&#xff08;\u6bcf\u4e2a\u53c2\u6570\u5bf9\u5e94\u4e00\u4e2a\u9879&#xff09;&#xff1b;\u5982\u679c\u6ca1\u6709\u53c2\u6570&#xff0c;\u7ec4 1 \u9ed8\u8ba4\u5230 0&#xff08;\u6574\u4e2a\u5339\u914d\u90fd\u88ab\u8fd4\u56de&#xff09;\u3002\u5982\u679c\u4e00\u4e2a\u7ec4 N \u53c2\u6570\u503c\u4e3a 0&#xff0c;\u76f8\u5e94\u7684\u8fd4\u56de\u503c\u5c31\u662f\u6574\u4e2a\u5339\u914d\u5b57\u7b26\u4e32&#xff1b;\u5982\u679c\u4e00\u4e2a\u7ec4\u53f7\u662f\u8d1f\u6570\u6216\u8d85\u51fa\u7ec4\u7684\u6700\u5927\u957f\u5ea6&#xff0c;\u5219\u7ed9\u51fa\u8d8a\u754c\u9519\u8bef&#xff1b;\u5982\u679c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u4e00\u4e2a\u7ec4\u5339\u914d\u4e86\u591a\u4e2a\u503c&#xff0c;\u5219\u76f8\u5e94\u7684\u7ec4\u53ea\u4fdd\u5b58\u6700\u540e\u5339\u914d\u7684\u503c\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">37<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>group?<br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\ngroup<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span>group1<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token builtin\">str<\/span> <span class=\"token keyword\">or<\/span> <span class=\"token builtin\">tuple<\/span><span class=\"token punctuation\">.<\/span><br \/>\nReturn subgroup<span class=\"token punctuation\">(<\/span>s<span class=\"token punctuation\">)<\/span> of the match by indices <span class=\"token keyword\">or<\/span> names<span class=\"token punctuation\">.<\/span><br \/>\nFor <span class=\"token number\">0<\/span> returns the entire match<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_function_or_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>group1<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5206\u7ec4\u5bf9\u5e94\u7684\u7f16\u53f7\u6216\u5206\u7ec4\u540d\u79f0\u3002<br \/>\n<span class=\"token number\">2<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u5339\u914d\u7684\u5b50\u7ec4\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u6307\u5b9a\u5206\u7ec4\u5bf9\u5e94\u7684\u4fe1\u606f\u3002\u4f7f\u7528Match.group()\u65b9\u6cd5\u83b7\u53d6\u6307\u5b9a\u5206\u7ec4\u5bf9\u5e94\u7684\u4fe1\u606f,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>group<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u65e0\u53c2\u6570\u6253\u5370\u5168\u90e8\u5185\u5bb9 MR_SHOP mr_shop<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>group<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u6307\u5b9a\u5206\u7ec4\u7f16\u53f7\u5bf9\u5e94\u5185\u5bb9 MR_SHOP<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>group<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;group_2&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u6307\u5b9a\u5206\u7ec4\u540d\u5bf9\u5e94\u5185\u5bb9 mr_shop<\/span><\/p>\n<h4>3.12.12 Match.groupdict()\u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u5305\u542b\u6240\u6709\u5339\u914d\u503c\u5206\u7ec4\u7684\u5b57\u5178<\/h4>\n<p>Match.groupdict() \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u4ee5\u5b57\u5178\u5f62\u5f0f\u8fd4\u56de\u5305\u542b\u6240\u6709\u5339\u914d\u503c\u5206\u7ec4\u7684\u4fe1\u606f&#xff0c;\u5176\u4e2d key \u5c31\u662f\u7ec4\u540d&#xff0c;value \u5c31\u662f\u5206\u7ec4\u5339\u914d\u7684\u4fe1\u606f\u5185\u5bb9\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">36<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>groupdict?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>groupdict<span class=\"token punctuation\">(<\/span>default<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">None<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span><br \/>\nReturn a dictionary containing <span class=\"token builtin\">all<\/span> the named subgroups of the match<span class=\"token punctuation\">,<\/span> keyed by the subgroup name<span class=\"token punctuation\">.<\/span><\/p>\n<p>default<br \/>\n  Is used <span class=\"token keyword\">for<\/span> groups that did <span class=\"token keyword\">not<\/span> participate <span class=\"token keyword\">in<\/span> the match<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_function_or_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>default<span class=\"token punctuation\">:<\/span> \u7528\u4e8e\u4e0d\u53c2\u4e0e\u5339\u914d\u7684\u60c5\u51b5&#xff0c;\u9ed8\u8ba4\u4e3a <span class=\"token boolean\">None<\/span>\u3002<br \/>\n<span class=\"token number\">2<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u4ee5\u5b57\u5178\u5f62\u5f0f\u8fd4\u56de\u5305\u542b\u6240\u6709\u5339\u914d\u503c\u5206\u7ec4\u7684\u4fe1\u606f\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u5b57\u5178\u5f62\u5f0f\u7684\u5206\u7ec4\u4fe1\u606f\u3002\u4f7f\u7528Match.groupdict()\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u5f62\u5f0f\u7684\u5206\u7ec4\u4fe1\u606f,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token comment\"># {&#039;group_1&#039;: &#039;MR_SHOP&#039;, &#039;group_2&#039;: &#039;mr_shop&#039;}<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>groupdict<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5b57\u5178\u5f62\u5f0f\u7684\u5206\u7ec4\u4fe1\u606f<\/span><\/p>\n<h4>3.12.13 Match.span()\u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u5305\u542b\u5f00\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\u7684\u4e8c\u5143\u7ec4<\/h4>\n<p>Match.span() \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u6307\u5b9a\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">35<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>span?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>span<span class=\"token punctuation\">(<\/span>group<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">\/<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> For match <span class=\"token builtin\">object<\/span> m<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">return<\/span> the <span class=\"token number\">2<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token builtin\">tuple<\/span> <span class=\"token punctuation\">(<\/span>m<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span>group<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> m<span class=\"token punctuation\">.<\/span>end<span class=\"token punctuation\">(<\/span>group<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_function_or_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>group<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5206\u7ec4\u540d\u6216\u8005\u5206\u7ec4\u540d\u5bf9\u5e94\u7684\u7f16\u53f7\u3002<br \/>\n<span class=\"token number\">2<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5f00\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\u7684\u4e8c\u5143\u7ec4\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b:\u83b7\u53d6\u6307\u5b9a\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\u3002\u4f7f\u7528Match.span()\u65b9\u6cd5\u83b7\u53d6\u6307\u5b9a\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>span<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5206\u7ec4\u7f16\u53f71\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0 (0, 7)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>span<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;group_2&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5206\u7ec4\u540dgroup_2\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0 (8, 15)<\/span><\/p>\n<h4>3.12.14 Match.start()\u65b9\u6cd5\u2014\u2014\u8fd4\u56de\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u6807\u8bb0<\/h4>\n<p>Match.start() \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u6807\u8bb0\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>In <span class=\"token punctuation\">[<\/span><span class=\"token number\">34<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>start?<br \/>\nSignature<span class=\"token punctuation\">:<\/span> match<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span>group<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">\/<\/span><span class=\"token punctuation\">)<\/span><br \/>\nDocstring<span class=\"token punctuation\">:<\/span> Return index of the start of the substring matched by group<span class=\"token punctuation\">.<\/span><br \/>\nType<span class=\"token punctuation\">:<\/span>      builtin_function_or_method<\/p>\n<p>\u53c2\u6570\u8bf4\u660e<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token number\">1.<\/span>group<span class=\"token punctuation\">:<\/span> \u8868\u793a\u5206\u7ec4\u540d\u6216\u8005\u5206\u7ec4\u540d\u5bf9\u5e94\u7684\u7f16\u53f7\u3002<br \/>\n<span class=\"token number\">2<\/span><span class=\"token punctuation\">.<\/span>\u8fd4\u56de\u503c<span class=\"token punctuation\">:<\/span> \u8fd4\u56de\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u6807\u8bb0\u3002<\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b1:\u83b7\u53d6\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0\u3002\u4f7f\u7528Match.start()\u65b9\u6cd5\u4e0eMatch.end()\u65b9\u6cd5\u83b7\u53d6\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>match<span class=\"token punctuation\">(<\/span>pattern<span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> match<span class=\"token punctuation\">.<\/span>end<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u5168\u90e8\u5206\u7ec4\u7684\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0 0 15<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> match<span class=\"token punctuation\">.<\/span>end<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u7f16\u53f71\u5206\u7ec4\u7684\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0 0 7<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> match<span class=\"token punctuation\">.<\/span>end<span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u7f16\u53f72\u5206\u7ec4\u7684\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0 8 15<\/span><br \/>\n<span class=\"token comment\"># \u6253\u5370\u5206\u7ec4\u540d\u7684\u5339\u914d\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5f00\u59cb\u3001\u7ed3\u675f\u6807\u8bb0<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;group_1&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> match<span class=\"token punctuation\">.<\/span>end<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;group_2&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># 0 15<\/span><\/p>\n<p><font size=\"3\" color=\"green\" face=\"\u9ed1\u4f53\">\u793a\u4f8b2:\u79fb\u9664\u8868\u8fbe\u5f0f\u641c\u7d22\u7684\u5185\u5bb9\u3002\u901a\u8fc7Match.start()\u65b9\u6cd5\u4e0eMatch.end()\u65b9\u6cd5\u79fb\u9664\u6307\u5b9a\u5185\u5bb9,\u4ee3\u7801\u5982\u4e0b:<\/font><\/p>\n<p><span class=\"token comment\"># -*- coding: utf-8 -*-<\/span><br \/>\n<span class=\"token comment\"># &#064;Time    : 2024-09-25 9:50<\/span><br \/>\n<span class=\"token comment\"># &#064;Author  : AmoXiang<\/span><br \/>\n<span class=\"token comment\"># &#064;File: re_demo.py<\/span><br \/>\n<span class=\"token comment\"># &#064;Software: PyCharm<\/span><br \/>\n<span class=\"token comment\"># &#064;Blog: https:\/\/blog.csdn.net\/xw1680<\/span><\/p>\n<p><span class=\"token keyword\">import<\/span> re  <span class=\"token comment\"># \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0fre\u6a21\u5757<\/span><\/p>\n<p>pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">compile<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;(?P&lt;group_1&gt;\\\\w&#043;) (?P&lt;group_2&gt;\\\\w&#043;)&#039;<\/span><span class=\"token punctuation\">,<\/span> re<span class=\"token punctuation\">.<\/span>I<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5206\u7ec4\u8868\u8fbe\u5f0f<\/span><br \/>\nstring <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;MR_SHOP mr_shop&#039;<\/span>  <span class=\"token comment\"># \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/span><br \/>\nmatch <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span>search<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;SHOP mr_&#039;<\/span><span class=\"token punctuation\">,<\/span> string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5339\u914d\u7ed3\u679cMatch\u5bf9\u8c61<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u539f\u5b57\u7b26\u4e32 MR_SHOP mr_shop<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>match<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u9700\u8981\u79fb\u9664\u7684\u5185\u5bb9 &lt;re.Match object; span&#061;(3, 11), match&#061;&#039;SHOP mr_&#039;&gt;<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>string<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">:<\/span>match<span class=\"token punctuation\">.<\/span>start<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#043;<\/span> string<span class=\"token punctuation\">[<\/span>match<span class=\"token punctuation\">.<\/span>end<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u79fb\u9664\u540e\u7684\u5185\u5bb9 MR_shop<\/span><\/p>\n<h2>\u56db\u3001\u7ec3\u4e60<\/h2>\n<p>\u300aPython\u7ec3\u4e60\u5b9d\u5178&#xff1a;Day 3 &#8211; \u9009\u62e9\u9898 &#8211; \u5b57\u7b26\u4e32\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u57fa\u7840\u7ec3\u4e60\u8865\u5145\u300b &#x1f449; \u67e5\u770b\u6587\u7ae0 \u53c2\u8003 \u4e8c\u3001\u5b57\u7b26\u4e32\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f \u4e00\u8282\u3002<\/p>\n<p>\u81f3\u6b64\u4eca\u5929\u7684\u5b66\u4e60\u5c31\u5230\u6b64\u7ed3\u675f\u4e86&#xff0c;\u7b14\u8005\u5728\u8fd9\u91cc\u58f0\u660e&#xff0c;\u7b14\u8005\u5199\u6587\u7ae0\u53ea\u662f\u4e3a\u4e86\u5b66\u4e60\u4ea4\u6d41&#xff0c;\u4ee5\u53ca\u8ba9\u66f4\u591a\u5b66\u4e60Python\u8bed\u8a00\u7684\u8bfb\u8005\u5c11\u8d70\u4e00\u4e9b\u5f2f\u8def&#xff0c;\u8282\u7701\u65f6\u95f4&#xff0c;\u5e76\u4e0d\u7528\u505a\u5176\u4ed6\u7528\u9014&#xff0c;\u5982\u6709\u4fb5\u6743&#xff0c;\u8054\u7cfb\u535a\u4e3b\u5220\u9664\u5373\u53ef\u3002\u611f\u8c22\u60a8\u9605\u8bfb\u672c\u7bc7\u535a\u6587&#xff0c;\u5e0c\u671b\u672c\u6587\u80fd\u6210\u4e3a\u60a8\u7f16\u7a0b\u8def\u4e0a\u7684\u9886\u822a\u8005\u3002\u795d\u60a8\u9605\u8bfb\u6109\u5feb&#xff01;<\/p>\n<hr \/>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014846-682fd3fe91512.gif\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u597d\u4e66\u4e0d\u538c\u8bfb\u767e\u56de&#xff0c;\u719f\u8bfb\u8bfe\u601d\u5b50\u81ea\u77e5\u3002\u800c\u6211\u60f3\u8981\u6210\u4e3a\u5168\u573a\u6700\u9753\u7684\u4ed4&#xff0c;\u5c31\u5fc5\u987b\u575a\u6301\u901a\u8fc7\u5b66\u4e60\u6765\u83b7\u53d6\u66f4\u591a\u77e5\u8bc6&#xff0c;\u7528\u77e5\u8bc6\u6539\u53d8\u547d\u8fd0&#xff0c;\u7528\u535a\u5ba2\u89c1\u8bc1\u6210\u957f&#xff0c;\u7528\u884c\u52a8\u8bc1\u660e\u6211\u5728\u52aa\u529b\u3002 \u00a0\u00a0\u00a0\u00a0\u5982\u679c\u6211\u7684\u535a\u5ba2\u5bf9\u4f60\u6709\u5e2e\u52a9\u3001\u5982\u679c\u4f60\u559c\u6b22\u6211\u7684\u535a\u5ba2\u5185\u5bb9&#xff0c;\u8bf7 \u70b9\u8d5e\u3001\u8bc4\u8bba\u3001\u6536\u85cf \u4e00\u952e\u4e09\u8fde\u54e6&#xff01;\u542c\u8bf4\u70b9\u8d5e\u7684\u4eba\u8fd0\u6c14\u4e0d\u4f1a\u592a\u5dee&#xff0c;\u6bcf\u4e00\u5929\u90fd\u4f1a\u5143\u6c14\u6ee1\u6ee1\u5466&#xff01;\u5982\u679c\u5b9e\u5728\u8981\u767d\u5ad6\u7684\u8bdd&#xff0c;\u90a3\u795d\u4f60\u5f00\u5fc3\u6bcf\u4e00\u5929&#xff0c;\u6b22\u8fce\u5e38\u6765\u6211\u535a\u5ba2\u770b\u770b\u3002 \u2003\u7f16\u7801\u4e0d\u6613&#xff0c;\u5927\u5bb6\u7684\u652f\u6301\u5c31\u662f\u6211\u575a\u6301\u4e0b\u53bb\u7684\u52a8\u529b\u3002\u70b9\u8d5e\u540e\u4e0d\u8981\u5fd8\u4e86 \u5173\u6ce8 \u6211\u54e6&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.1k\u6b21\uff0c\u70b9\u8d5e36\u6b21\uff0c\u6536\u85cf36\u6b21\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u5feb\u901f\u63d0\u53d6\u3001\u9a8c\u8bc1\u3001\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u590d\u6742\u6a21\u5f0f\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u96f6\u638c\u63e1 Python \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u89c4\u5219\uff0c\u5e76\u7ed3\u5408 re \u6a21\u5757\u7684\u6838\u5fc3\u51fd\u6570\uff0c\u6df1\u5165\u5256\u6790\u5982\u4f55\u9ad8\u6548\u5b8c\u6210\u6587\u672c\u5339\u914d\u3001\u63d0\u53d6\u4e0e\u66ff\u6362\u7b49\u64cd\u4f5c\uff0c\u52a9\u4f60\u63d0\u5347\u5b57\u7b26\u4e32\u5904\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"author":2,"featured_media":39243,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3506,81,3507,94],"topic":[],"class_list":["post-39261","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-100python","tag-python","tag-re","tag-94"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u300a100\u5929\u7cbe\u901aPython\u2014\u2014\u57fa\u7840\u7bc7 2025 \u7b2c18\u5929\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5165\u95e8\u5b9e\u6218\uff0c\u89e3\u9501\u5b57\u7b26\u4e32\u5904\u7406\u7684\u9b54\u6cd5\u529b\u91cf\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/39261.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u300a100\u5929\u7cbe\u901aPython\u2014\u2014\u57fa\u7840\u7bc7 2025 \u7b2c18\u5929\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5165\u95e8\u5b9e\u6218\uff0c\u89e3\u9501\u5b57\u7b26\u4e32\u5904\u7406\u7684\u9b54\u6cd5\u529b\u91cf\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.1k\u6b21\uff0c\u70b9\u8d5e36\u6b21\uff0c\u6536\u85cf36\u6b21\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u5feb\u901f\u63d0\u53d6\u3001\u9a8c\u8bc1\u3001\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u590d\u6742\u6a21\u5f0f\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u96f6\u638c\u63e1 Python \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u89c4\u5219\uff0c\u5e76\u7ed3\u5408 re \u6a21\u5757\u7684\u6838\u5fc3\u51fd\u6570\uff0c\u6df1\u5165\u5256\u6790\u5982\u4f55\u9ad8\u6548\u5b8c\u6210\u6587\u672c\u5339\u914d\u3001\u63d0\u53d6\u4e0e\u66ff\u6362\u7b49\u64cd\u4f5c\uff0c\u52a9\u4f60\u63d0\u5347\u5b57\u7b26\u4e32\u5904\u7406\u80fd\u529b\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/39261.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-23T01:48:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014844-682fd3fcdc750.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"25 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39261.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/39261.html\",\"name\":\"\u300a100\u5929\u7cbe\u901aPython\u2014\u2014\u57fa\u7840\u7bc7 2025 \u7b2c18\u5929\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5165\u95e8\u5b9e\u6218\uff0c\u89e3\u9501\u5b57\u7b26\u4e32\u5904\u7406\u7684\u9b54\u6cd5\u529b\u91cf\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-23T01:48:47+00:00\",\"dateModified\":\"2025-05-23T01:48:47+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/39261.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/39261.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39261.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u300a100\u5929\u7cbe\u901aPython\u2014\u2014\u57fa\u7840\u7bc7 2025 \u7b2c18\u5929\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5165\u95e8\u5b9e\u6218\uff0c\u89e3\u9501\u5b57\u7b26\u4e32\u5904\u7406\u7684\u9b54\u6cd5\u529b\u91cf\u300b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u300a100\u5929\u7cbe\u901aPython\u2014\u2014\u57fa\u7840\u7bc7 2025 \u7b2c18\u5929\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5165\u95e8\u5b9e\u6218\uff0c\u89e3\u9501\u5b57\u7b26\u4e32\u5904\u7406\u7684\u9b54\u6cd5\u529b\u91cf\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/39261.html","og_locale":"zh_CN","og_type":"article","og_title":"\u300a100\u5929\u7cbe\u901aPython\u2014\u2014\u57fa\u7840\u7bc7 2025 \u7b2c18\u5929\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5165\u95e8\u5b9e\u6218\uff0c\u89e3\u9501\u5b57\u7b26\u4e32\u5904\u7406\u7684\u9b54\u6cd5\u529b\u91cf\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.1k\u6b21\uff0c\u70b9\u8d5e36\u6b21\uff0c\u6536\u85cf36\u6b21\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u5feb\u901f\u63d0\u53d6\u3001\u9a8c\u8bc1\u3001\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u590d\u6742\u6a21\u5f0f\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u96f6\u638c\u63e1 Python \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u89c4\u5219\uff0c\u5e76\u7ed3\u5408 re \u6a21\u5757\u7684\u6838\u5fc3\u51fd\u6570\uff0c\u6df1\u5165\u5256\u6790\u5982\u4f55\u9ad8\u6548\u5b8c\u6210\u6587\u672c\u5339\u914d\u3001\u63d0\u53d6\u4e0e\u66ff\u6362\u7b49\u64cd\u4f5c\uff0c\u52a9\u4f60\u63d0\u5347\u5b57\u7b26\u4e32\u5904\u7406\u80fd\u529b\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/39261.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-23T01:48:47+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014844-682fd3fcdc750.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"25 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/39261.html","url":"https:\/\/www.wsisp.com\/helps\/39261.html","name":"\u300a100\u5929\u7cbe\u901aPython\u2014\u2014\u57fa\u7840\u7bc7 2025 \u7b2c18\u5929\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5165\u95e8\u5b9e\u6218\uff0c\u89e3\u9501\u5b57\u7b26\u4e32\u5904\u7406\u7684\u9b54\u6cd5\u529b\u91cf\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-23T01:48:47+00:00","dateModified":"2025-05-23T01:48:47+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/39261.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/39261.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/39261.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u300a100\u5929\u7cbe\u901aPython\u2014\u2014\u57fa\u7840\u7bc7 2025 \u7b2c18\u5929\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5165\u95e8\u5b9e\u6218\uff0c\u89e3\u9501\u5b57\u7b26\u4e32\u5904\u7406\u7684\u9b54\u6cd5\u529b\u91cf\u300b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=39261"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39261\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/39243"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=39261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=39261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=39261"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=39261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}