{"id":4132,"date":"2018-10-24T21:31:15","date_gmt":"2018-10-24T13:31:15","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4132"},"modified":"2018-10-24T21:31:15","modified_gmt":"2018-10-24T13:31:15","slug":"go%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a04-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4132.html","title":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60#4-\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u6700\u8fd1\u5728\u8ddf\u7740\u90dd\u6797\u8001\u5e08\u7684\u300aGo\u8bed\u8a00\u6838\u5fc336\u8bb2\u300b\u8fd9\u95e8\u8bfe\u7a0b\u91cd\u62feGo\u8bed\u8a00\u5b66\u4e60\uff08\u4e4b\u524d\u572815\u5e74\u7684\u65f6\u5019\u6211\u81ea\u5b66\u8fc7\u4e00\u6bb5\u65f6\u95f4\uff0c\u4e0d\u8fc7\u540e\u6765\u6ca1\u600e\u4e48\u7528Go\u6765\u5199\u4ee3\u7801\uff0c\u6240\u4ee5\u6709\u4e9b\u5185\u5bb9\u90fd\u5fd8\u4e86\uff0c\u8fd9\u6b21\u5e0c\u671b\u80fd\u575a\u6301\u4e0b\u53bb\uff09\uff0c\u6240\u4ee5\u51c6\u5907\u628a\u4e00\u4e9b\u5e38\u89c1\u529f\u80fd\u7528Go\u8bed\u8a00\u8bd5\u7740\u5b9e\u8df5\u4e00\u904d\uff0c\u52a0\u5f3a\u5b66\u4e60\u6548\u679c\u548c\u5bf9\u8fd9\u95e8\u8bed\u8a00\u7684\u611f\u89c9\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<ul>\n<li>func Compile(expr string) (*Regexp, error) \/\/<strong>Compile\u5728\u89e3\u6790\u8868\u8fbe\u5f0f\u6210\u529f\u7684\u60c5\u51b5\u4e0b\u4f1a\u8fd4\u56de\u4e00\u4e2aRegexp\u5bf9\u8c61<\/strong><\/li>\n<li><\/li>\n<li>func MustCompile(str string) *Regexp \/\/<span style=\"color: #ff0000;\"><strong>MustCompile\u548cCompile\u5f88\u50cf\uff0c\u4f46\u662f\u4f1a\u5728\u8868\u8fbe\u5f0f\u65e0\u6cd5\u89e3\u6790\u7684\u60c5\u51b5\u4e0b\u629b\u51fapanic<\/strong><\/span><\/li>\n<li><\/li>\n<li>func MatchString(pattern string, s string) (matched bool, err error) \/\/<strong>MatchString\u8fd4\u56detrue\/false\u8868\u793a\u662f\u5426\u5339\u914d<\/strong><\/li>\n<li><\/li>\n<li>func (re *Regexp) FindString(s string) string \/\/<strong><span style=\"color: #ff0000;\">FindString\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8be5\u5b57\u7b26\u4e32\u4fdd\u5b58\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u6700\u5de6\u5339\u914d\u5230\u7684\u6587\u672c<\/span>\u3002<\/strong>\u5982\u679c\u6ca1\u6709\u5339\u914d\uff0c\u5219\u8fd4\u56de\u503c\u662f\u7a7a\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u5982\u679c\u6b63\u5219\u8868\u8fbe\u5f0f\u6210\u529f\u5339\u914d\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219\u8fd4\u56de\u503c\u4e5f\u662f\u7a7a\u7684\u3002\u5982\u679c\u9700\u8981\u533a\u5206\u8fd9\u4e9b\u60c5\u51b5\uff0c\u8bf7\u4f7f\u7528FindStringIndex\u6216FindStringSubmatch\u65b9\u6cd5<\/li>\n<li><\/li>\n<li>func (re *Regexp) FindAllString(s string, n int) []string \/\/<span style=\"color: #ff0000;\"><strong>FindAllString\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5339\u914d\u9879\u7684\u5b57\u7b26\u4e32\u5207\u7247<\/strong><\/span><\/li>\n<li><\/li>\n<li>func (re *Regexp) Split(s string, n int) []string \/\/<strong>Split\u7528\u4f20\u5165\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8868\u793a\u5206\u9694\u7b26\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5207\u5206\uff0c\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u5207\u7247\u4f5c\u4e3a\u7ed3\u679c<\/strong><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">s := regexp.MustCompile(\"a*\").Split(\"abaabaccadaaae\", 5)\r\n\/\/ s: [\"\", \"b\", \"b\", \"c\", \"cadaaae\"]\r\n\r\nre := regexp.MustCompile(\"fo.?\")\r\nfmt.Printf(\"%q\\n\", re.FindString(\"seafood\")) \/\/ \"foo\"\r\nfmt.Printf(\"%q\\n\", re.FindString(\"meat\")) \/\/ \"\"\r\n<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"regexp\"\r\n)\r\n\r\nfunc main() {\r\n    \/\/ someRegex, _ := regexp.Compile(`[^aouiye]([aouiye])([^aouiye])?`) \/\/ \u6ce8\u610f\u300c\u53cd\u5f15\u53f7\u300d\r\n    \/\/ matches := someRegex.FindAllStringSubmatch(\"somestri\", -1)\r\n    \/\/ fmt.Printf(\"%q\\n\", matches)\r\n\r\n    text := `Hello \u4e16\u754c\uff01123 Go.`\r\n\r\n    \/\/ \u67e5\u627e\u8fde\u7eed\u7684\u5c0f\u5199\u5b57\u6bcd\r\n    reg := regexp.MustCompile(`[a-z]+`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"ello\" \"o\"]\r\n\r\n    \/\/ \u67e5\u627e\u8fde\u7eed\u7684\u975e\u5c0f\u5199\u5b57\u6bcd\r\n    reg = regexp.MustCompile(`[^a-z]+`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"H\" \" \u4e16\u754c\uff01123 G\" \".\"]\r\n\r\n    \/\/ \u67e5\u627e\u8fde\u7eed\u7684\u5355\u8bcd\u5b57\u6bcd\r\n    reg = regexp.MustCompile(`[\\w]+`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Hello\" \"123\" \"Go\"]\r\n\r\n    \/\/ \u67e5\u627e\u8fde\u7eed\u7684\u975e\u5355\u8bcd\u5b57\u6bcd\u3001\u975e\u7a7a\u767d\u5b57\u7b26\r\n    reg = regexp.MustCompile(`[^\\w\\s]+`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"\u4e16\u754c\uff01\" \".\"]\r\n\r\n    \/\/ \u67e5\u627e\u8fde\u7eed\u7684\u5927\u5199\u5b57\u6bcd\r\n    reg = regexp.MustCompile(`[[:upper:]]+`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"H\" \"G\"]\r\n\r\n    \/\/ \u67e5\u627e\u8fde\u7eed\u7684\u975e ASCII \u5b57\u7b26\r\n    reg = regexp.MustCompile(`[[:^ascii:]]+`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"\u4e16\u754c\uff01\"]\r\n\r\n    \/\/ \u67e5\u627e\u8fde\u7eed\u7684\u6807\u70b9\u7b26\u53f7\r\n    reg = regexp.MustCompile(`[\\pP]+`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"\uff01\" \".\"]\r\n\r\n    \/\/ \u67e5\u627e\u8fde\u7eed\u7684\u975e\u6807\u70b9\u7b26\u53f7\u5b57\u7b26\r\n    reg = regexp.MustCompile(`[\\PP]+`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Hello \u4e16\u754c\" \"123 Go\"]\r\n\r\n    \/\/ \u67e5\u627e\u8fde\u7eed\u7684\u6c49\u5b57\r\n    reg = regexp.MustCompile(`[\\p{Han}]+`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"\u4e16\u754c\"]\r\n\r\n    \/\/ \u67e5\u627e\u8fde\u7eed\u7684\u975e\u6c49\u5b57\u5b57\u7b26\r\n    reg = regexp.MustCompile(`[\\P{Han}]+`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Hello \" \"\uff01123 Go.\"]\r\n\r\n    \/\/ \u67e5\u627e Hello \u6216 Go\r\n    reg = regexp.MustCompile(`Hello|Go`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Hello\" \"Go\"]\r\n\r\n    \/\/ \u67e5\u627e\u884c\u9996\u4ee5 H \u5f00\u5934\uff0c\u4ee5\u7a7a\u683c\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\r\n    reg = regexp.MustCompile(`^H.*\\s`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Hello \u4e16\u754c\uff01123 \"]\r\n\r\n    \/\/ \u67e5\u627e\u884c\u9996\u4ee5 H \u5f00\u5934\uff0c\u4ee5\u7a7a\u767d\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\uff08\u975e\u8d2a\u5a6a\u6a21\u5f0f\uff09\r\n    reg = regexp.MustCompile(`(?U)^H.*\\s`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Hello \"]\r\n\r\n    \/\/ \u67e5\u627e\u4ee5 hello \u5f00\u5934\uff08\u5ffd\u7565\u5927\u5c0f\u5199\uff09\uff0c\u4ee5 Go \u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\r\n    reg = regexp.MustCompile(`(?i:^hello).*Go`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Hello \u4e16\u754c\uff01123 Go\"]\r\n\r\n    \/\/ \u67e5\u627e Go.\r\n    reg = regexp.MustCompile(`\\QGo.\\E`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Go.\"]\r\n\r\n    \/\/ \u67e5\u627e\u4ece\u884c\u9996\u5f00\u59cb\uff0c\u4ee5\u7a7a\u683c\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\uff08\u975e\u8d2a\u5a6a\u6a21\u5f0f\uff09\r\n    reg = regexp.MustCompile(`(?U)^.* `)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Hello \"]\r\n\r\n    \/\/ \u67e5\u627e\u4ee5\u7a7a\u683c\u5f00\u5934\uff0c\u5230\u884c\u5c3e\u7ed3\u675f\uff0c\u4e2d\u95f4\u4e0d\u5305\u542b\u7a7a\u683c\u5b57\u7b26\u4e32\r\n    reg = regexp.MustCompile(` [^ ]*$`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\" Go.\"]\r\n\r\n    \/\/ \u67e5\u627e\u201c\u5355\u8bcd\u8fb9\u754c\u201d\u4e4b\u95f4\u7684\u5b57\u7b26\u4e32\r\n    reg = regexp.MustCompile(`(?U)\\b.+\\b`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Hello\" \" \u4e16\u754c\uff01\" \"123\" \" \" \"Go\"]\r\n\r\n    \/\/ \u67e5\u627e\u8fde\u7eed 1 \u6b21\u5230 4 \u6b21\u7684\u975e\u7a7a\u683c\u5b57\u7b26\uff0c\u5e76\u4ee5 o \u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\r\n    reg = regexp.MustCompile(`[^ ]{1,4}o`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Hello\" \"Go\"]\r\n\r\n    \/\/ \u67e5\u627e Hello \u6216 Go\r\n    reg = regexp.MustCompile(`(?:Hell|G)o`)\r\n    fmt.Printf(\"%q\\n\", reg.FindAllString(text, -1))\r\n    \/\/ [\"Hello\" \"Go\"]\r\n\r\n    \/\/ \u67e5\u627e Hello \u6216 Go\uff0c\u66ff\u6362\u4e3a Hellooo\u3001Gooo\r\n    \/\/ reg = regexp.MustCompile(`(?PHell|G)o`)\r\n    \/\/ fmt.Printf(\"%q\\n\", reg.ReplaceAllString(text, \"${n}ooo\"))\r\n    \/\/ \"Hellooo \u4e16\u754c\uff01123 Gooo.\"\r\n\r\n    \/\/ \u4ea4\u6362 Hello \u548c Go\r\n    reg = regexp.MustCompile(`(Hello)(.*)(Go)`)\r\n    fmt.Printf(\"%q\\n\", reg.ReplaceAllString(text, \"$3$2$1\"))\r\n    \/\/ \"Go \u4e16\u754c\uff01123 Hello.\"\r\n\r\n    \/\/ \u7279\u6b8a\u5b57\u7b26\u7684\u67e5\u627e\r\n    reg = regexp.MustCompile(`[\\f\\t\\n\\r\\v\\123\\x7F\\x{10FFFF}\\\\\\^\\$\\.\\*\\+\\?\\{\\}\\(\\)\\[\\]\\|]`)\r\n    fmt.Printf(\"%q\\n\", reg.ReplaceAllString(\"\\f\\t\\n\\r\\v\\123\\x7F\\U0010FFFF\\\\^$.*+?{}()[]|\", \"-\"))\r\n    \/\/ \"----------------------\"\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/www.cnblogs.com\/golove\/p\/3269099.html\"><strong>\u57fa\u7840\u77e5\u8bc6 &#8211; Golang \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/a><\/li>\n<li><a href=\"https:\/\/github.com\/StefanSchroeder\/Golang-Regex-Tutorial\/tree\/master\/zh\">Golang \u6b63\u5219\u8868\u8fbe\u5f0f\u6559\u7a0b<\/a><\/li>\n<li><a href=\"https:\/\/golang.org\/pkg\/regexp\/\">https:\/\/golang.org\/pkg\/regexp\/<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/45856464\/golang-why-does-regexp-findallstringsubmatch-returns-string-and-not-str\">https:\/\/stackoverflow.com\/questions\/45856464\/golang-why-does-regexp-findallstringsubmatch-returns-string-and-not-str<\/a><\/li>\n<li><a href=\"https:\/\/gobyexample.com\/regular-expressions\">https:\/\/gobyexample.com\/regular-expressions<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6700\u8fd1\u5728\u8ddf\u7740\u90dd\u6797\u8001\u5e08\u7684\u300aGo\u8bed\u8a00\u6838\u5fc336\u8bb2\u300b\u8fd9\u95e8\u8bfe\u7a0b\u91cd\u62feGo\u8bed\u8a00\u5b66\u4e60\uff08\u4e4b\u524d\u572815\u5e74\u7684\u65f6\u5019 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[564,377],"class_list":["post-4132","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-golang","tag-regex"],"views":2469,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4132"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4132\/revisions"}],"predecessor-version":[{"id":4133,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4132\/revisions\/4133"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}