{"id":2108,"date":"2024-04-20T13:29:09","date_gmt":"2024-04-20T05:29:09","guid":{"rendered":"https:\/\/www.xfan.top\/?p=2108"},"modified":"2024-04-20T13:58:25","modified_gmt":"2024-04-20T05:58:25","slug":"re-python-group","status":"publish","type":"post","link":"https:\/\/www.xfan.top\/index.php\/2024\/04\/20\/2108\/","title":{"rendered":"re &#8212; \u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c python \u5339\u914d\u5bf9\u8c61\u5b50\u7ec4"},"content":{"rendered":"<h2>\u5339\u914d\u5bf9\u8c61\u7684\u5b50\u7ec4 <code>Match.group([group1, ...])<\/code><\/h2>\n<pre><code>&gt;&gt;&gt; m = re.match(r\"(\\w+) (\\w+)\", \"Isaac Newton, physicist\")\n&gt;&gt;&gt; m.group(0)       # The entire match\n'Isaac Newton'\n&gt;&gt;&gt; m.group(1)       # The first parenthesized subgroup.\n'Isaac'\n&gt;&gt;&gt; m.group(2)       # The second parenthesized subgroup.\n'Newton'\n&gt;&gt;&gt; m.group(1, 2)    # Multiple arguments give us a tuple.\n('Isaac', 'Newton')<\/code><\/pre>\n<p>\u7531\u62ec\u53f7\u5305\u542b\u7684\u8868\u8fbe\u5f0f\u4e3a\u4e00\u4e2a\u5b50\u7ec4\uff0c\u5982\u4e0a\u8ff0\u4ee3\u7801\u7684<code>(\\w+)<\/code>\uff0c\u6839\u636e\u6240\u5728\u4f4d\u7f6e\uff0c\u53ef\u4f7f\u7528<code>.group(index)<\/code><code>index\u22081,2<\/code>\u6765\u83b7\u53d6\u3002<\/p>\n<h2>\u4e3a\u5b50\u7ec4\u547d\u540d <code>(?P&lt;name&gt;...)<\/code><\/h2>\n<p>\u4f7f\u7528<code>(?P&lt;name&gt;...)<\/code><\/p>\n<pre><code>&gt;&gt;&gt; m = re.match(r\"(?P&lt;first_name&gt;\\w+) (?P&lt;last_name&gt;\\w+)\", \"Malcolm Reynolds\")\n&gt;&gt;&gt; m.group('first_name')\n'Malcolm'\n&gt;&gt;&gt; m.group('last_name')\n'Reynolds'<\/code><\/pre>\n<h2>\u5143\u7ec4\u5f62\u5f0f\u83b7\u53d6\u6240\u6709\u5b50\u7ec4\u7684\u5339\u914d <code>Match.groups(default=None)<\/code><\/h2>\n<pre><code>&gt;&gt;&gt; m = re.match(r\"(\\d+)\\.(\\d+)\", \"24.1632\")\n&gt;&gt;&gt; m.groups()\n('24', '1632')<\/code><\/pre>\n<h2>\u5b57\u5178\u5f62\u5f0f\u83b7\u53d6\u6240\u6709\u5b50\u7ec4\u7684\u5339\u914d <code>Match.groupdict(default=None)<\/code><\/h2>\n<pre><code>&gt;&gt;&gt; m = re.match(r\"(?P&lt;first_name&gt;\\w+) (?P&lt;last_name&gt;\\w+)\", \"Malcolm Reynolds\")\n&gt;&gt;&gt; m.groupdict()\n{'first_name': 'Malcolm', 'last_name': 'Reynolds'}<\/code><\/pre>\n<h2>\u5b50\u7ec4\u5339\u914d\u5230\u7684\u5b57\u4e32\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u6807\u53f7 <code>Match.start([group])<\/code> <code>Match.end([group])<\/code><\/h2>\n<p><code>m.string[m.start(g):m.end(g)]<\/code>\u7b49\u4ef7\u4e8e<code>m.group(g)<\/code><\/p>\n<h2>\u5143\u7ec4\u5f62\u5f0f\u7684\u5b50\u7ec4\u5339\u914d\u5230\u7684\u5b57\u4e32\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u6807\u53f7<code>Match.span([group])<\/code><\/h2>\n<p>\u8fd4\u56de\u4e00\u4e2a\u4e8c\u5143\u7ec4 <code>(m.start(group), m.end(group))<\/code> \u3002 \u5982\u679c group \u6ca1\u6709\u5728\u8fd9\u4e2a\u5339\u914d\u4e2d\uff0c\u5c31\u8fd4\u56de (-1, -1) \u3002group \u9ed8\u8ba4\u4e3a0\uff0c\u5c31\u662f\u6574\u4e2a\u5339\u914d\u3002<\/p>\n<hr \/>\n<p><a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/re.html\">[1]<\/a> Python re \u6587\u6863 <a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/re.html\">https:\/\/docs.python.org\/zh-cn\/3\/library\/re.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5339\u914d\u5bf9\u8c61\u7684\u5b50\u7ec4 Match.group([group1, &#8230;]) &gt;&gt;&gt; m = re. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-2108","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/comments?post=2108"}],"version-history":[{"count":12,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2108\/revisions"}],"predecessor-version":[{"id":2120,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2108\/revisions\/2120"}],"wp:attachment":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/media?parent=2108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/categories?post=2108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/tags?post=2108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}