{"id":208002,"date":"2025-07-08T14:14:27","date_gmt":"2025-07-08T06:14:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208002\/"},"modified":"2025-07-08T14:14:27","modified_gmt":"2025-07-08T06:14:27","slug":"golang-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e4%b8%8e-replaceallstring-%e8%bf%9b%e8%a1%8c%e5%a4%9a%e4%b8%aa%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208002\/","title":{"rendered":"Golang \u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e `ReplaceAllString` \u8fdb\u884c\u591a\u4e2a\u5339\u914d"},"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 \u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e `ReplaceAllString` \u8fdb\u884c\u591a\u4e2a\u5339\u914d<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 19:57:41<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728Golang\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aGolang \u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e `ReplaceAllString` \u8fdb\u884c\u591a\u4e2a\u5339\u914d\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u7f16\u5199\u4e00\u4e9b\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u548c\u5206\u79bb\u770b\u8d77\u6765\u50cf\u8f6f\u4ef6\u7248\u672c\u7684\u5b57\u7b26\u4e32\uff08\u4f46\u5b83\u4eec\u5b9e\u9645\u4e0a\u5e76\u975e\u5982\u6b64\uff0c\u56e0\u6b64\u4f8b\u5982 semver \u89e3\u6790\u5c06\u65e0\u6cd5\u5de5\u4f5c\uff09\u3002\u6211\u5728\u5339\u914d\u8f93\u5165\u5b57\u7b26\u4e32\u7684\u201c\u524d\u7f00\u201d\u548c\u201c\u540e\u7f00\u201d\u90e8\u5206\u65f6\u9047\u5230\u4e00\u4e9b\u95ee\u9898\u3002<\/p>\n<p>\u6211\u4ece\u4e0b\u9762\u5f97\u5230\u7684\u8f93\u51fa\u5f88\u5947\u602a..<\/p>\n<pre>\/\/ Sample inputs:\n\/\/ * '1.2.3-thing' (Prefix: '1.2.3', Suffix: '-thing')\n\/\/ * '1.2.3+1' (Prefix: '1.2.3', Suffix: '+1')\n\/\/ * '1.2.3' (Prefix: '1.2.3', Suffix: '')\n\/\/ * '1' (Prefix: '1', Suffix: '')\n\/\/ * '1-x' (Prefix: '1', Suffix: '-x')\n\/\/ * '1-x-x' (Prefix: '1', Suffix: '-x-x')\n\/\/ * '1.2.3-thing.1' (Prefix: '1.2.3', Suffix: '-thing.1')\n\/\/ * '1.2-thing-1' (Prefix: '1.2', Suffix: '-thing-1')\n\/\/ * 'k1.2.3-thing' (Prefix: 'k1.2.3', Suffix: '-thing')\n\/\/ * 'k-thing-x' (Prefix: 'k', Suffix: '-thing-x')\n\/\/\nfunc InspectVersionTag(tag string) {\n    re := regexp.MustCompile(`^([^\\-]+)([\\-+].+)$`)\n    suffix := \"\"\n    if re.MatchString(tag) {\n        tag = re.ReplaceAllString(tag, `$1`)\n        suffix = re.ReplaceAllString(tag, `$2`)\n    }\n    fmt.Println(fmt.Sprintf(\"Prefix is: %s\", tag))\n    fmt.Println(fmt.Sprintf(\"Suffix is: %s\", suffix))\n}\n\n\/\/ Current sample output\n\/\/\n\/\/ Input: 1.2.3+1\n\/\/ Prefix is: 1.2.3\n\/\/ Suffix is: 1.2.3<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6839\u636e\u60a8\u7684\u793a\u4f8b\uff0c\u8fd9\u5e94\u8be5\u975e\u5e38\u7b80\u5355\u3002\u6211\u4ece\u5934\u5230\u5c3e\u5199\u4e0b\u4e86\u4ee5\u4e0b\u4ee3\u7801\u3002\u5b83\u751a\u81f3\u53ef\u80fd\u65e0\u6cd5\u7f16\u8bd1\uff0c\u4f46\u4f60\u5e94\u8be5\u660e\u767d\uff1a<\/p>\n<pre>func parseVersion(ver string) (prefix, suffix string) {\n    parts := strings.SplitAfter(ver, \"-\", 2)\n    if len(parts) == 1 {\n        parts = strings.SplitAfter(ver, \"+\", 2)\n    }\n    if len(parts) == 1 {\n        return ver, \"\"\n    }\n    return parts[0], parts [1]\n}<\/pre>\n<p>\u60a8\u7edd\u5bf9\u5e94\u8be5\u5728\u81ea\u52a8\u6d4b\u8bd5\u4e2d\u5217\u51fa\u6240\u6709\u793a\u4f8b\uff0c\u800c\u4e0d\u662f\u6ce8\u91ca\u3002\u5982\u679c\u4e0a\u8ff0\u4ee3\u7801\u6ca1\u6709\u5e2e\u52a9\uff0c\u5219\u8bf4\u660e\u60a8\u7684\u793a\u4f8b\u4e0d\u591f\u73b0\u5b9e\u3002<\/p>\n<p>\u8fd9\u662f\u6211\u89e3\u51b3\u7c7b\u4f3c\u95ee\u9898\u7684\u65b9\u6cd5\u3002\u5728\u8fd9\u91cc\u67e5\u770b <code>test_mkparser_pkgbasepattern<\/code> \u548c <code>test_mkparser_dependency<\/code>\uff1a<\/p>\n<\/p>\n<p>\u5b83\u5f88\u5bb9\u6613\u53d8\u5f97\u590d\u6742\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u60a8\u5e94\u8be5\u4ece\u4e00\u5f00\u59cb\u5c31\u4e3a\u6bcf\u4e2a\u6709\u8da3\u7684\u6848\u4f8b\u7f16\u5199\u4e00\u4e2a\u6d4b\u8bd5\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aGolang \u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e `ReplaceAllString` \u8fdb\u884c\u591a\u4e2a\u5339\u914d\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/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-208002","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208002","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=208002"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208002\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}