{"id":50,"date":"2026-03-14T22:30:57","date_gmt":"2026-03-14T14:30:57","guid":{"rendered":"http:\/\/47.118.30.30\/?p=50"},"modified":"2026-03-15T11:40:56","modified_gmt":"2026-03-15T03:40:56","slug":"python%e6%ad%a3%e5%88%99%e5%ba%93%e4%bb%a5%e5%8f%8a%e6%ad%a3%e5%88%99%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86","status":"publish","type":"post","link":"https:\/\/arknight.wiki\/index.php\/2026\/03\/14\/python%e6%ad%a3%e5%88%99%e5%ba%93%e4%bb%a5%e5%8f%8a%e6%ad%a3%e5%88%99%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86\/","title":{"rendered":"python\u6b63\u5219\u5e93\u4ee5\u53ca\u6b63\u5219\u57fa\u7840\u77e5\u8bc6"},"content":{"rendered":"<h1>python\u6b63\u5219\u5e93re\u57fa\u7840\u77e5\u8bc6<\/h1>\n<h2>\u6b63\u5219\u8bed\u6cd5\uff1a<\/h2>\n<p>[ABC]<\/p>\n<pre><code>\u5339\u914d[\u2026]\u4e2d\u7684\u6240\u6709\u5b57\u7b26\n[A-Z]\uff1a\u5339\u914dA-Z\u5927\u5199\u5b57\u6bcd\n.:\u4efb\u610f\u5b57\u7b26\uff0c\u9664\u6362\u884c\u7b26<\/code><\/pre>\n<p>[sS]<\/p>\n<pre><code>\u5339\u914d\u6240\u6709\ns:\u5339\u914d\u6240\u6709\u7a7a\u767d\u7b26\nS:\u975e\u7a7a\u767d\u7b26\uff0c\u4e0d\u5305\u62ec\u6362\u884c\n\u7a7a\u767d\u7b26\u7684\u5b9a\u4e49:\n\u7a7a\u683c\uff08 \uff09\n\u5236\u8868\u7b26\uff08t\uff09\n\u6362\u884c\u7b26\uff08n\uff09\n\u6362\u9875\u7b26\uff08f\uff09\n\u5782\u76f4\u5236\u8868\u7b26\uff08v\uff09<\/code><\/pre>\n<p>w<\/p>\n<pre><code>\u5339\u914d\u5b57\u6bcd\u3001\u4e0b\u5212\u7ebf\u3001\u6570\u5b57\n\u4e0e[A-Za-z0-9_]\u7b49\u4ef7<\/code><\/pre>\n<p>d<\/p>\n<pre><code>\u5339\u914d\u4efb\u610f\u4e00\u4e2a\u963f\u62c9\u4f2f\u6570\u5b57\n\u76f8\u5f53\u4e8e [0-9]<\/code><\/pre>\n<p>\/i<\/p>\n<pre><code>\u5ffd\u7565\u5927\u5c0f\u5199<\/code><\/pre>\n<p>\/g<\/p>\n<pre><code>\u5168\u5c40\u5339\u914d\uff0c\u627e\u5230\u6240\u6709\u5339\u914d\u9879<\/code><\/pre>\n<p>\u9650\u5b9a\u7b26\u53f7<\/p>\n<pre><code>*:\u5339\u914d\u96f6\u6b21\u4ee5\u4e0a\n+:\u5339\u914d\u4e00\u6b21\u4ee5\u4e0a\n?:\u96f6\u6b21\u6216\u4e00\u6b21\n{n}:\u5339\u914dn\u6b21\n{n,}:\u5339\u914dn\u6b21\u4ee5\u4e0a\n{n,m}:\u5339\u914dn\u5230m\u6b21<\/code><\/pre>\n<p>\u5b9a\u4f4d\u7b26\uff1a<\/p>\n<pre><code>^\uff1a\u5339\u914d\u8f93\u5165\u5b57\u7b26\u4e32\u5f00\u59cb\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u8bbe\u7f6e\u4e86 RegExp \u5bf9\u8c61\u7684 Multiline \u5c5e\u6027\uff0c^ \u8fd8\u4f1a\u4e0e n \u6216 r \u4e4b\u540e\u7684\u4f4d\u7f6e\u5339\u914d\n$\uff1a\u5339\u914d\u8f93\u5165\u5b57\u7b26\u4e32\u7ed3\u5c3e\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u8bbe\u7f6e\u4e86 RegExp \u5bf9\u8c61\u7684 Multiline \u5c5e\u6027\uff0c$ \u8fd8\u4f1a\u4e0e n \u6216 r \u4e4b\u524d\u7684\u4f4d\u7f6e\u5339\u914d\nb\uff1a\u5339\u914d\u4e00\u4e2a\u5355\u8bcd\u8fb9\u754c\uff0c\u5373\u5b57\u4e0e\u7a7a\u683c\u95f4\u7684\u4f4d\u7f6e\nB\uff1a\u975e\u5355\u8bcd\u8fb9\u754c\u5339\u914d\n\n\/\/\u4e0d\u80fd\u5c06\u9650\u5b9a\u7b26\u4e0e\u5b9a\u4f4d\u7b26\u4e00\u8d77\u4f7f\u7528<\/code><\/pre>\n<p>\u9009\u62e9\uff1a<\/p>\n<pre><code>():\u6355\u83b7\u7ec4\uff0c\u6709\u6355\u83b7\u7ec4\u540e\u8fd4\u56de\u6355\u83b7\u7ec4\u91cc\u7684\u5185\u5bb9\n\/\/ptn_get = re.compile(br\"$_GET['(w+)']\")\u8fd4\u56de\u53c2\u6570\n\nexp1(?=exp2)\uff1a\u67e5\u627eexp2\u524d\u9762\u7684exp1\n(?&lt;=exp2)exp1\uff1a\u67e5\u627e exp2 \u540e\u9762\u7684 exp1\nexp1(?!exp2)\uff1a\u67e5\u627e\u540e\u9762\u4e0d\u662f exp2 \u7684 exp1\n(?&lt;!exp2)exp1\uff1a\u67e5\u627e\u524d\u9762\u4e0d\u662f exp2 \u7684 exp1<\/code><\/pre>\n<h2>re\u5e93\uff1a<\/h2>\n<p>\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408\u67d0\u79cd\u6a21\u5f0f\uff1a<\/p>\n<pre><code>import re\nif re.match(r'^d+$', '12345'):\n    print(\"\u5168\u662f\u6570\u5b57\")<\/code><\/pre>\n<p>\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u7b26\u5408\u6a21\u5f0f\u7684\u5185\u5bb9\uff1a<\/p>\n<pre><code>result = re.search(r'd+', '\u4ef7\u683c\u662f100\u5143')\nif result:\n    print(result.group())  # \u8f93\u51fa: 100<\/code><\/pre>\n<p>\u67e5\u627e\u6240\u6709\u5339\u914d\u9879\uff1a<\/p>\n<pre><code>numbers = re.findall(r'd+', 'a1b22c333')\nprint(numbers)  # ['1', '22', '333']<\/code><\/pre>\n<p>\u66ff\u6362\u5b57\u7b26\uff1a<\/p>\n<pre><code>text = re.sub(r'd+', 'X', 'a1b22c333')\nprint(text)  # aXbXcX<\/code><\/pre>\n<p>\u5206\u5272\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>parts = re.split(r'W+', 'hello, world!')\nprint(parts)  # ['hello', 'world', '']<\/code><\/pre>\n<p>\u91cd\u590d\u4f7f\u7528\uff1a<\/p>\n<pre><code>\u57fa\u672c\u5339\u914d\nimport re\n# \u7f16\u8bd1\u4e00\u4e2a\u5339\u914d\u90ae\u7bb1\u7684\u6b63\u5219\nemail_pattern = re.compile(r'b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z|a-z]{2,}b')\ntext = \"\u8054\u7cfb\u6211\uff1auser@example.com \u6216 admin@test.org\"\n# \u4f7f\u7528\u7f16\u8bd1\u540e\u7684\u5bf9\u8c61\u8fdb\u884c\u67e5\u627e\nmatches = email_pattern.findall(text)\nprint(matches)  # ['user@example.com', 'admin@test.org']\n\n\u5e26\u6807\u5fd7\u7684\u7f16\u8bd1\uff08\u5982\u5ffd\u7565\u5927\u5c0f\u5199\uff09\npattern = re.compile(r'hello', re.IGNORECASE)\nprint(pattern.search(\"HELLO world\"))  # \u5339\u914d\u6210\u529f\n\n\u591a\u6b21\u4f7f\u7528\u540c\u4e00\u4e2a\u6a21\u5f0f\uff08\u6027\u80fd\u4f18\u52bf\uff09\ndigits = re.compile(r'd+')\nlines = [\"abc123\", \"def456\", \"xyz789\"]\nfor line in lines:\n    match = digits.search(line)\n    if match:\n        print(match.group())\n\n\u7f16\u8bd1\u540e\u5bf9\u8c61\u7684\u5e38\u7528\u65b9\u6cd5\uff1a\n.match(string)\uff1a\u4ece\u5b57\u7b26\u4e32\u5f00\u5934\u5339\u914d\n.search(string)\uff1a\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u7b2c\u4e00\u4e2a\u5339\u914d\n.findall(string)\uff1a\u8fd4\u56de\u6240\u6709\u975e\u91cd\u53e0\u5339\u914d\u7684\u5217\u8868\n.finditer(string)\uff1a\u8fd4\u56de\u5339\u914d\u7684\u8fed\u4ee3\u5668\uff08Match \u5bf9\u8c61\uff09\n.sub(repl, string)\uff1a\u66ff\u6362\u5339\u914d\u9879\n.split(string)\uff1a\u6309\u5339\u914d\u9879\u5206\u5272\u5b57\u7b26\u4e32<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>python\u6b63\u5219\u5e93re\u57fa\u7840\u77e5\u8bc6 \u6b63\u5219\u8bed\u6cd5\uff1a [ABC] \u5339\u914d[\u2026]\u4e2d\u7684\u6240\u6709\u5b57\u7b26 [A-Z]\uff1a\u5339\u914dA-Z\u5927\u5199\u5b57\u6bcd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-50","post","type-post","status-publish","format-standard","hentry","category-3"],"_links":{"self":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":1,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"predecessor-version":[{"id":55,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts\/50\/revisions\/55"}],"wp:attachment":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}