{"id":207269,"date":"2025-07-08T09:18:02","date_gmt":"2025-07-08T01:18:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207269\/"},"modified":"2025-07-08T09:18:02","modified_gmt":"2025-07-08T01:18:02","slug":"golang-%e5%a4%8d%e6%9d%82%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e4%b8%8e-findallstringsubmatch","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207269\/","title":{"rendered":"Golang \u590d\u6742\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e FindAllStringSubmatch"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Golang \u590d\u6742\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e FindAllStringSubmatch<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 11:30:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang \u590d\u6742\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e FindAllStringSubmatch\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a<em>\u8d85\u7ea7\u82f1\u96c4<\/em>\u5b57\u7b26\u4e32\uff0c\u4ed6\u4eec<strong>\u6240\u6709\u4eba<\/strong>\u90fd\u6709<em>\u540d\u5b57<\/em>\uff0c\u4f46\u662f<strong>\u5e76\u975e\u6240\u6709\u4eba<\/strong>\u90fd\u6709\u5c5e\u6027\u3002<\/p>\n<p>\u5b83\u7684\u683c\u5f0f\u4e3a <code>nameattrnameattrdata<\/code>\uff0c\u5176\u4e2d <code>attrnameattrdata<\/code> \u662f\u53ef\u9009\u7684\u3002<\/p>\n<p>\u56e0\u6b64\uff0c<em>superheroes<\/em> \u5b57\u7b26\u4e32\u662f\uff1a<\/p>\n<p><code>\u8d85\u4eba\u886c\u886b\u84dd\u8272\u5c0f\u4e11\u8718\u86db\u4fa0\u5e74\u9f8415yogirlfriendcindy<\/code><\/p>\n<p>\u6211\u60f3\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u7ed3\u679c\u586b\u5145\u5230<em>\u5730\u56fe\u5207\u7247<\/em>\u4e2d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>[ {name: superman, shirt: blue},\n  {name: joker},\n  {name: spiderman, age: 15yo, girlfriend: cindy} ]<\/pre>\n<p>\u6211\u65e0\u6cd5\u5728 go \u6f14\u793a\u4e2d\u5b8c\u6210\u5b83\u3002\u6211\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f <code>(\\\\w+)(?:(\\\\w+)(\\\\w+))*<\/code>\uff0c\u4f46\u5b83\u53ea\u80fd\u6355\u83b7\u5355\u4e2a\u5c5e\u6027\uff0c\u5373\u6b63\u5219\u8868\u8fbe\u5f0f\u65e0\u6cd5\u6355\u83b7 <code>age<\/code> \u5c5e\u6027\u3002 p&gt; <\/p>\n<p>\u6211\u7684\u4ee3\u7801\u662f\uff1a<\/p>\n<pre>func main() {\n    re := regexp.mustcompile(\"(\\\\w+)(?:(\\\\w+)(\\\\w+))*\")\n    fmt.printf(\"%q\\n\", re.findallstringsubmatch(\"supermanshirtbluejokerspidermanage15yogirlfriendcindy\", -1))\n}<\/pre>\n<p>go \u6f14\u793a\u4ee3\u7801\u5728\u8fd9\u91cc\uff1ahttps:\/\/play.golang.org\/p\/epv66lvwurk<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>[\n    [\"supermanshirtblue\" \"superman\" \"shirt\" \"blue\"]\n    [\"joker\" \"joker\" \"\" \"\"]\n    [\"spidermanage15yogirlFriendCindy\" \"spiderman\" \"girlFriend\" \"Cindy\"]\n]<\/pre>\n<p><code>age<\/code> \u4e22\u5931\u4e86\uff0c\u77e5\u9053\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u65e0\u6cd5\u4f7f\u7528\u5355\u4e2a\u6355\u83b7\u7ec4\u6355\u83b7\u4efb\u610f\u6570\u91cf\u7684\u5b50\u5b57\u7b26\u4e32\u3002\u60a8\u9700\u8981\u9996\u5148\u5339\u914d\u6574\u4e2a\u8bb0\u5f55\uff0c\u7136\u540e\u5c06\u5176\u5b50\u90e8\u5206\u4e0e\u53e6\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3002<\/p>\n<p>\u67e5\u770b\u793a\u4f8b\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"regexp\"\n)\n\nfunc main() {\n\n    str := \"supermanshirtbluejokerspidermanage15yogirlfriendcindy\"\n\n    re_main := regexp.mustcompile(`(\\w+)((?:\\w+\\w+)*)`)\n    re_aux := regexp.mustcompile(`(\\w+)(\\w+)`)\n    for _, match := range re_main.findallstringsubmatch(str, -1) {\n        fmt.printf(\"%v\\n\", match[1])\n        for _, match_aux := range re_aux.findallstringsubmatch(match[2], -1) {      \n            fmt.printf(\"%v: %v\\n\", match_aux[1], match_aux[2])\n        }\n        fmt.println(\"--end of match--\") \n    }  \n}<\/pre>\n<p>\u53c2\u89c1<\/p>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>superman\nshirt: blue\n--end of match--\njoker\n--end of match--\nspiderman\nage: 15yo\ngirlfriend: cindy\n--end of match--<\/pre>\n<p>\u8fd9\u91cc\uff0c<code>(\\w+)((?:\\w+\\w+)*)<\/code> \u662f\u4e3b\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5b83\u5339\u914d\u5e76\u6355\u83b7\u7b2c 1 \u7ec4\u4e3b\u201c\u952e\u201d\u548c\u53e6\u4e00\u4e2a\u952e\u7684\u5b57\u7b26\u4e32 -\u503c\u88ab\u6355\u83b7\u5230\u7b2c 2 \u7ec4\u4e2d\u3002\u7136\u540e\uff0c\u60a8\u9700\u8981\u8fed\u4ee3\u627e\u5230\u7684\u5339\u914d\u9879\uff0c\u5e76\u4f7f\u7528 <code>(\\w+)(\\w+)<\/code> \u4ece\u7b2c 2 \u7ec4\u6536\u96c6\u6240\u6709\u952e\u503c\u3002<\/p>\n<p>\u60a8\u5df2\u5c06\u60a8\u7684 <code>regex<\/code> \u8bbe\u7f6e\u4e3a <code>(\\\\w+)(?:(\\\\w+)(\\\\w+))*<\/code>\uff0c\u5b83\u4ec5\u6253\u5370\u4e24\u4e2a\u7ea7\u522b\u7684 <code>key<\/code> \u548c <code>value<\/code>\uff0c\u5c31\u50cf\u5b83\u6839\u636e\u60a8\u7684 <code>regex<\/code> \u6253\u5370\u4e00\u6837\uff1a <\/p>\n<pre>[[\"supermanshirtblue\" \"superman\" \"shirt\" \"blue\"]\n[\"joker\" \"joker\" \"\" \"\"]\n[\"spidermanage15yogirlcindy\" \"spiderman\" \"girl\" \"cindy\"]]<\/pre>\n<p>\u6211\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u518d\u589e\u52a0\u4e00\u5bf9 <code>key<\/code> \u548c <code>value<\/code> \u5bf9\uff0c\u5b83\u4e5f\u4f1a\u6253\u5370 <code>age<\/code> \u503c\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b <code>regex<\/code> \u4ee3\u7801\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<pre>re := regexp.MustCompile(\"(\\\\w+)(?:(\\\\w+)(\\\\w+))*(?:(\\\\w+)(\\\\w+))*\")\n    fmt.Printf(\"%q\\n\", re.FindAllStringSubmatch(\"supermanshirtbluejokerspidermanage15yogirlCindy\", -1))<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aGolang \u590d\u6742\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e FindAllStringSubmatch\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207269","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207269"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207269\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}