{"id":75651,"date":"2024-03-01T15:44:18","date_gmt":"2024-03-01T07:44:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=75651"},"modified":"2024-03-01T15:44:33","modified_gmt":"2024-03-01T07:44:33","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8go%e8%af%ad%e8%a8%80%e4%b8%ad%e4%bd%bf%e7%94%a8%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/75651.html","title":{"rendered":"\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-75652\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/03\/img-NfFmia4cTvMLgv47vQVHazYA.png?x-oss-process=image\/auto-orient,1\/format,webp\" width=\"600\" height=\"342\" alt=\"\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\" \/><\/p>\n<h2><strong># \u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>\u5728Go\u8bed\u8a00\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5<strong>\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u641c\u7d22\u3001\u66ff\u6362\u3001\u5339\u914d<\/strong>\u7b49\u64cd\u4f5c\u3002Go\u6807\u51c6\u5e93\u4e2d\u7684`regexp`\u5305\u63d0\u4f9b\u4e86\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\uff0c\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u5728Go\u7a0b\u5e8f\u4e2d\u65b9\u4fbf\u5730\u4f7f\u7528\u5b83\u4eec\u3002<strong>\u9996\u5148\uff0c\u4f60\u9700\u8981\u4e86\u89e3\u5982\u4f55\u7f16\u8bd1\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff0c\u8fd9\u662f\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b2c\u4e00\u6b65\u3002\u7f16\u8bd1\u540e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u5339\u914d\u5b57\u7b26\u4e32\u6216\u8005\u4ece\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u7279\u5b9a\u6a21\u5f0f\u3002\u6b64\u5916\uff0c`regexp`\u5305\u8fd8\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u67e5\u627e\u5339\u914d\u9879\u3001\u66ff\u6362\u5339\u914d\u5230\u7684\u6587\u672c\u7b49\u3002<\/p>\n<p>\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4f7f\u7528`regexp`\u5305\u4e2d\u7684`Compile`\u51fd\u6570\u6216\u5176\u53d8\u4f53`MustCompile`\u6765\u5b8c\u6210\u7684\u3002`Compile`\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u7f16\u8bd1\u540e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u548c\u4e00\u4e2a\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u3002\u800c`MustCompile`\u5219\u662f`Compile`\u7684\u7b80\u5316\u7248\uff0c\u5b83\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u8bd1\u5931\u8d25\u65f6\u4f1a\u5f15\u53d1panic\uff0c\u9002\u7528\u4e8e\u90a3\u4e9b\u6b63\u5219\u8868\u8fbe\u5f0f\u786e\u5b9a\u65e0\u8bef\u7684\u573a\u666f\u3002<\/p>\n<p>## \u4e00\u3001\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<p>\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4f7f\u7528Go\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b2c\u4e00\u6b65\u3002\u4f7f\u7528`regexp`\u5305\u4e2d\u7684`Compile`\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u8bd1\u6210\u4e00\u4e2a`*Regexp`\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u540e\u7eed\u7684\u5339\u914d\u3001\u67e5\u627e\u7b49\u64cd\u4f5c\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u7f16\u8bd1\u4e00\u4e2a\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u6570\u5b57\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<p>&#8220;`go<\/p>\n<p>package m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/p>\n<p>import (<\/p>\n<p>    &#8220;fmt&#8221;<\/p>\n<p>    &#8220;regexp&#8221;<\/p>\n<p>)<\/p>\n<p>func main() {<\/p>\n<p>    re, err := regexp.Compile(`\\d+`)<\/p>\n<p>    if err != nil {<\/p>\n<p>        fmt.Println(&#8220;\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u51fa\u9519:&#8221;, err)<\/p>\n<p>        return<\/p>\n<p>    }<\/p>\n<p>    fmt.Println(&#8220;\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u8bd1\u6210\u529f:&#8221;, re)<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c`\\d+`\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u3002`Compile`\u51fd\u6570\u8fd4\u56de\u4e86\u8fd9\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7f16\u8bd1\u7248\u672c\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u63a5\u4e0b\u6765\u7684\u5339\u914d\u64cd\u4f5c\u3002<\/p>\n<p>## \u4e8c\u3001\u5339\u914d\u5b57\u7b26\u4e32<\/p>\n<p>\u4e00\u65e6\u7f16\u8bd1\u597d\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5339\u914d\u5b57\u7b26\u4e32\u4e86\u3002`*Regexp`\u7c7b\u578b\u63d0\u4f9b\u4e86\u591a\u79cd\u5339\u914d\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5982`MatchString`\u3001`FindString`\u3001`FindStringSubmatch`\u7b49\u3002<\/p>\n<p>&#8211; `MatchString`\u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>&#8211; `FindString`\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u3002<\/p>\n<p>&#8211; `FindStringSubmatch`\u5219\u8fd4\u56de\u5339\u914d\u7684\u5b57\u7b26\u4e32\u53ca\u5176\u5b50\u5339\u914d\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528`FindString`\u65b9\u6cd5\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u6570\u5b57\uff1a<\/p>\n<p>&#8220;`go<\/p>\n<p>matched := re.FindString(&#8220;\u6211\u7684\u7535\u8bdd\u53f7\u7801\u662f123456&#8221;)<\/p>\n<p>fmt.Println(&#8220;\u627e\u5230\u7684\u5339\u914d\u9879:&#8221;, matched)<\/p>\n<p>&#8220;`<\/p>\n<p>## \u4e09\u3001\u67e5\u627e\u6240\u6709\u5339\u914d\u9879<\/p>\n<p>\u5728\u4e00\u4e9b\u573a\u666f\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u627e\u5230\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u9879\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528`FindAllString`\u65b9\u6cd5\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u5207\u7247\uff0c\u5305\u542b\u6240\u6709\u7684\u5339\u914d\u9879\u3002<\/p>\n<p>&#8220;`go<\/p>\n<p>allMatches := re.FindAllString(&#8220;\u6211\u7684\u7535\u8bdd\u53f7\u7801\u662f123456\uff0c\u4f60\u7684\u7535\u8bdd\u53f7\u7801\u662f7890&#8221;, -1)<\/p>\n<p>fmt.Println(&#8220;\u6240\u6709\u5339\u914d\u9879:&#8221;, allMatches)<\/p>\n<p>&#8220;`<\/p>\n<p>## \u56db\u3001\u66ff\u6362\u5339\u914d\u9879<\/p>\n<p>\u9664\u4e86\u67e5\u627e\u5339\u914d\u9879\uff0c\u4f60\u53ef\u80fd\u8fd8\u9700\u8981\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u3002`*Regexp`\u7c7b\u578b\u7684`ReplaceAllString`\u65b9\u6cd5\u5141\u8bb8\u4f60\u5c06\u6240\u6709\u5339\u914d\u7684\u90e8\u5206\u66ff\u6362\u4e3a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5c06\u6587\u672c\u4e2d\u7684\u6240\u6709\u6570\u5b57\u66ff\u6362\u4e3a`[\u6570\u5b57]`\uff1a<\/p>\n<p>&#8220;`go<\/p>\n<p>replaced := re.ReplaceAllString(&#8220;\u6211\u7684\u7535\u8bdd\u53f7\u7801\u662f123456&#8221;, &#8220;[\u6570\u5b57]&#8221;)<\/p>\n<p>fmt.Println(&#8220;\u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32:&#8221;, replaced)<\/p>\n<p>&#8220;`<\/p>\n<p>## \u4e94\u3001\u9ad8\u7ea7\u5339\u914d\u548c\u66ff\u6362\u6280\u5de7<\/p>\n<p>\u5728Go\u7684`regexp<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: Go\u8bed\u8a00\u4e2d\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff1f<\/strong><\/p>\n<p><strong>A: \u5728Go\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684regexp\u5305\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165regexp\u5305\u3002\u7136\u540e\u4f7f\u7528regexp.Compile()\u51fd\u6570\u6765\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5f97\u5230\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u8be5\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u5339\u914d\u64cd\u4f5c\uff0c\u6bd4\u5982FindString()\u3001FindStringIndex()\u7b49\u3002\u6700\u540e\uff0c\u8bb0\u5f97\u5904\u7406\u5339\u914d\u7ed3\u679c\u5e76\u68c0\u67e5\u9519\u8bef\u3002<\/strong><\/p>\n<p><strong>Q: \u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u5b9e\u73b0\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5206\u7ec4\u6355\u83b7\uff1f<\/strong><\/p>\n<p><strong>A: \u5728Go\u8bed\u8a00\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u62ec\u53f7\u6765\u8fdb\u884c\u5206\u7ec4\u6355\u83b7\u3002\u901a\u8fc7\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u5c0f\u62ec\u53f7\uff0c\u53ef\u4ee5\u5c06\u76f8\u5e94\u7684\u5b50\u8868\u8fbe\u5f0f\u5206\u7ec4\uff0c\u5e76\u5728\u5339\u914d\u6210\u529f\u540e\u53ef\u4ee5\u901a\u8fc7FindStringSubmatch()\u7b49\u65b9\u6cd5\u6765\u83b7\u53d6\u5206\u7ec4\u6355\u83b7\u7684\u7ed3\u679c\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u51fa\u5339\u914d\u5230\u7684\u7279\u5b9a\u90e8\u5206\uff0c\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3002<\/strong><\/p>\n<p><strong>Q: Go\u8bed\u8a00\u4e2d\u5982\u4f55\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u66ff\u6362\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p><strong>A: \u5728Go\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528regexp\u5305\u4e2d\u7684ReplaceAllString()\u51fd\u6570\u6765\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u66ff\u6362\u64cd\u4f5c\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6a21\u5f0f\u3001\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u4ee5\u53ca\u5f85\u5339\u914d\u7684\u76ee\u6807\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u76ee\u6807\u5b57\u7b26\u4e32\u4e2d\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u90e8\u5206\u66ff\u6362\u4e3a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u3002\u5728\u66ff\u6362\u64cd\u4f5c\u4e4b\u524d\uff0c\u8bb0\u5f97\u5148\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u5e76\u68c0\u67e5\u9519\u8bef\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"# \u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f \u5728Go\u8bed\u8a00\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u641c\u7d22\u3001\u66ff\u6362\u3001\u5339\u914d\u7b49\u64cd\u4f5c\u3002Go\u6807 [&hellip;]","protected":false},"author":1,"featured_media":75654,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/75651"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=75651"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/75651\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/75654"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=75651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=75651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=75651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}