This is a creation in Article, where the information may have evolved or changed.
General overview
If you are unfamiliar with the syntax of a regular expression, you can execute the following command:
go doc regexp/syntax
Code Learning
Package Mainimport ("FMT" "RegExp") func Expandtest () {Pat: = ' ((ABC.) def.) GHI) ' Reg: = RegExp. Mustcompile (PAT) FMT. Println (Reg. Numsubexp ()) src: = []byte (' Abc-def-ghi abc+def+ghi ') Template: = []byte (' $ $ $ $ $ ')//replace first match result Mat CH: = Reg. Findsubmatchindex (SRC) fmt. Printf ("%v\n", Match)//[0 0 0 8 0 4] DST: = Reg. Expand (nil, template, SRC, match) fmt. Printf ("%s\n\n", DST)//Abc-def-ghi Abc-def-ghi abc-def-abc-//Replace all matching results for _, Match: = Range Reg. Findallsubmatchindex (SRC,-1) {FMT. Printf ("%v\n", match) DST: = Reg. Expand (nil, template, SRC, match) fmt. Printf ("%s\n", DST)}//[0 0 0 8 0 4]//Abc-def-ghi Abc-def-ghi abc-def-abc-//[12 23 12 23 12 //Abc+def+ghi Abc+def+ghi abc+def+ abc+}func testfind () {Re: = RegExp. Mustcompile ("A*r") fmt. Println (String (re). Find ([]byte ("Paranoabrmal"))) Fmt. Println (re. Numsubexp ()) Rep: = RegExp. MustcompiLeposix ("A*r|ara") fmt. Println (String (Rep. Find ([]byte ("Paranoabrmal"))) Fmt. Println (Rep. Numsubexp ()) B: = []byte ("ABC1DEF1") Pat: = ' abc1|abc1def1 ' reg1: = RegExp. Mustcompile (PAT)//First match REG2: = RegExp. Mustcompileposix (PAT)//Longest match FMT. Printf ("%s\n", REG1. Find (b))//ABC1 FMT. Printf ("%s\n", REG2. Find (b))//Abc1def1 FMT. Println (REG1. Numsubexp ()) b = []byte ("ABC1DEF1") Pat = ' (abc|abc1def) * * REG1 = RegExp. Mustcompile (PAT)//First match REG2 = RegExp. Mustcompileposix (PAT)//Longest match FMT. Printf ("%s\n", REG1. Find (b))//ABC1 FMT. Printf ("%s\n", REG2. Find (b))//Abc1def1 FMT. Println (REG1. Numsubexp ())}func Testfindall () {Re: = RegExp. Mustcompile ("Ar") fmt. Printf ("%q\n", (re). FindAll ([]byte ("Paranoarmal"),-1)) Rep: = RegExp. Mustcompileposix ("Ar") fmt. Printf ("%q\n", (Rep. FindAll ([]byte ("Paranoarmal"),-1)) Pat: = ' ((ABC.) def.) GHI) ' src: = []byte (' Abc-def-ghi Abc+def+ghi ') Reg: = RegExp. Mustcompile (PAT) Fmt. Printf ("%q\n", (REG). Find (SRC))) fmt. Printf ("%q\n", (REG). FINDALL (SRC,-1))) REGP: = RegExp. Mustcompileposix (PAT) FMT. Printf ("%q\n", (REGP). Find (SRC))) fmt. Printf ("%q\n", (REGP). FINDALL (SRC,-1)))}func Testreg (Pat, srcstr string) {fmt. Println ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") src: = []byte (srcstr) Reg: = RegExp. Mustcompile (PAT) FMT. Printf ("%q\n", (REG). Find (SRC))) fmt. Printf ("%q\n", (REG). FindString (SRCSTR))/* Find returns the regular match result of leftmost, which is the shortest matching string that satisfies the leftmost match, Testreg (' ((ABC.) def.) GHI) x* ', ' abc-def-ghixxa Abc+def+ghixx ') results: "Abc-def-ghixx" Analysis: * indicates 0 or more, preference for multiple, so priority match for "Abc-def-ghixx" Testreg (' (ABC.) def.) GHI) ', ' Abc-def-ghixxa Abc+def+ghixx ') results: "Abc-def-ghi" Analysis: the leftmost shortest match that satisfies the match is "Abc-def-ghi" Testreg (' A (x*) b (y|z) c ', '-axxxbyc--abzc-') results: "AXXXBYC" Analysis: the leftmost shortest match that satisfies a match is "Abc-def-ghi" Testreg (' a*r ', ' paranoabrmal ') Result: "Ar" Analysis: The leftmost shortest match that satisfies the match is "ar" */FMT. Printf ("%q\n", (REG). FINDALL (SRC,-1))) FMT. Printf ("%q\n", (REG). Findallstring (Srcstr,-1))/* FindAll is the "all" version of Find and returns the shortest match of adjacent non-reciprocal overrides Testreg (((ABC.) def.) GHI) x* ', ' abc-def-ghixxa Abc+def+ghixx ') results: ["Abc-def-ghixx" "Abc+def+ghixx"] Parse: Return all occurrences Testreg (' (ABC.) def.) GHI) ', ' Abc-def-ghixxa Abc+def+ghixx ') results: ["Abc-def-ghi" "Abc+def+ghi"] Parse: Return all occurrences Testre G (' A (x*) b (y|z) c ', '-axxxbyc--abzc-') results: ["AXXXBYC" "ABZC"] Analysis: Return all occurrences Testreg (' a*r ', ' PA Ranoabrmal ') results: ["AR" "R"] Analysis: * Represents 0 or more, so the second one matches only R */FMT. Printf ("%d\n", (REG). FindIndex (SRC))) fmt. Printf ("%d\n", (REG). Findstringindex (SRCSTR))/* FindIndex is the "Index" version of Find, matches the position in the original string, and returns nil indicating no matches found. Testreg (' (ABC.) def.) GHI) x* ', ' abc-def-ghixxa Abc+def+ghixx ') results: [0 13] Analysis: ' Abc-def-ghixxa abc+def+ghixx ' [0:13]Is "Abc-def-ghixx" Testreg ((ABC.) def.) GHI) ', ' Abc-def-ghixxa Abc+def+ghixx ') results: [0 11] Analysis: Ibid. testreg (' A (x*) b (y|z) c ', '-axxxbyc- -abzc-') Results: [1 8] Analysis: Ibid. testreg (' a*r ', ' paranoabrmal ') results: [1 3] Analysis : Ditto */FMT. Printf ("%d\n", (REG). Findallindex (SRC,-1))) FMT. Printf ("%d\n", (REG). Findallstringindex (Srcstr,-1))/* Findallindex is the "Index" version of FindAll, which indicates the position of the match in the original string, and returns nil indicating that no match was found. Testreg (' (ABC.) def.) GHI) x* ', ' abc-def-ghixxa Abc+def+ghixx ') results: [[0 13] [15 28]] Analysis: ' Abc-def-ghixxa abc+def+ghixx ' [0:13] Is "Abc-def-ghixx" ' Abc-def-ghixxa Abc+def+ghixx ' [15:28] is "Abc+def+ghixx" Testreg (((ABC.) def.) GHI) ', ' Abc-def-ghixxa Abc+def+ghixx ') results: [[0 11] [15 26]] Analysis: ibid. Testreg (' A (x*) b (y|z) C '), '-axxxbyc--abzc-') results: [[1 8] [11 15]] Analysis: Ibid. testreg (' a*r ', ' paranoabrmal ') Results: [[1 3] [8 9]] Analysis: IBID. */FMT. PRINTLN ("Begin Submatch") fmt. Printf ("%q\n", (REG). Findsubmatch (SRC))) fmt. Printf ("%q\n", (REG). Findstringsubmatch (SRCSTR))/* Findsubmatch is the "Submatch" version of Find, which returns a sub-match of the regular expression. A sub-match refers to the Zhong expression within the parentheses, which is labeled from left to right. Sub-match 0 indicates all expressions, sub-match 1 indicates a subexpression within the 1th parenthesis, and so on. Testreg (' (ABC.) def.) GHI) x* ', ' abc-def-ghixxa Abc+def+ghixx ') results: ["Abc-def-ghixx" "Abc-def-ghi" "abc-def-" "abc-"] Analysis: "." matches any character. Submatch0 as ' ((ABC.) def.) GHI) x* ', Submatch1 as ' ((ABC.) def.) GHI) ', Submatch2 as ' (ABC.) def.) ', Submatch3 for ' (ABC.) ', Testreg ((ABC.) def.) GHI) ', ' Abc-def-ghixxa Abc+def+ghixx ') results: ["Abc-def-ghi" "Abc-def-ghi" "abc-def-" "abc-"] Analysis: "." matches any character. Submatch0 as ' ((ABC.) def.) GHI) ', Submatch1 as ' ((ABC.) def.) GHI) ', Submatch2 as ' (ABC.) def.) ', Submatch3 for ' (ABC.) ', Testreg (' A (x*) b (y|z) c ', '-axxxbyc--abzc-') results: ["AXXXBYC" "xxx" "Y"] Analysis: Submatch0 for ' A (x *) b (y|z) C ', Submatch1 for ' (x*) ', sUbmatch2 for ' (y|z) ' Testreg (' a*r ', ' paranoabrmal ') results: ["AR"] Analysis: Submatch0 as ' a*r ' */FMT . Printf ("%d\n", (REG). Findsubmatchindex (SRC))) fmt. Printf ("%d\n", (REG). Findstringsubmatchindex (SRCSTR))///Findsubmatchindex is the index version of Findsubmatch, which returns the end-to-end subscript Testreg (' (((AB C.) def.) GHI) x* ', ' abc-def-ghixxa Abc+def+ghixx ') results: [0 13 0 11 0 8 0 4] Analysis: ' Abc-def-ghixxa Abc+def+ghixx ' [0 : 13]= "Abc-def-ghixx" ' Abc-def-ghixxa abc+def+ghixx ' [0:11]= "Abc-def-ghi" ' Abc-def-ghixxa Abc+def+ghixx ' [0:8]= ' abc-def-"' Abc-def-ghixxa abc+def+ghixx ' [0:4]=" abc-"Testreg (' ((ABC.) def.) GHI) ', ' Abc-def-ghixxa Abc+def+ghixx ') results: [0 11 0 11 0 8 0 4] Analysis: Ibid. testreg (' A (x*) b (y|z) c ', '-axxxbyc--abzc-') results: [1 8 2 5 6 7] Analysis: The same as Testreg (' a*r ', ' paranoabrmal ') knot Fruit: [1 3] Analysis: Ibid. */FMT. Printf ("%q\n", (REG). FindAllsubmatch (SRC,-1))) FMT. Printf ("%q\n", (REG). Findallstringsubmatch (Srcstr,-1))/* Findallsubmatch is the "all" version of Findsubmatch, which returns the child matches of all occurrences. Sub-match 0 indicates all expressions, sub-match 1 indicates a subexpression within the 1th parenthesis, and so on. Testreg (' (ABC.) def.) GHI) x* ', ' abc-def-ghixxa Abc+def+ghixx ') results: [["Abc-def-ghixx" "Abc-def-ghi" "abc-def-" "abc-"] ["Abc+def+ghixx" "Abc+def+ghi" "abc+def+" "abc+"]] Analysis: Reference Findsubmatch Testreg (' ((ABC.) def.) GHI) ', ' Abc-def-ghixxa Abc+def+ghixx ') results: [["Abc-def-ghi" "Abc-def-ghi" "abc-def-" "abc-"] ["Abc+def+ghi" "abc+ Def+ghi "" abc+def+ "" abc+ "]] Analysis: Reference Findsubmatch Testreg (' A (x*) b (y|z) c ', '-axxxbyc--abzc-') Results: [["AXXXBYC" "xxx" "Y"] ["ABZC" "" "Z"]] Analysis: Refer to Findsubmatch testreg (' a*r ', ' paranoabrmal ') Results: [[AR] [[]]] Analysis: Refer to Findsubmatch */FMT. Printf ("%d\n", (REG). Findallsubmatchindex (SRC,-1))) FMT. Printf ("%d\n", (REG). Findallstringsubmatchindex (Srcstr,-1))) /* Findallsubmatchindex is the index version of Findallsubmatch, which returns the end-to-end subscript of the match Testreg ((ABC.) def.) GHI) x* ', ' abc-def-ghixxa Abc+def+ghixx ') results: [[0 13 0 11 0 8 0 4] [15 28 15 26 15 23 15 19]] Analysis: Reference fi Ndsubmatchindex Testreg (' (ABC.) def.) GHI) ', ' Abc-def-ghixxa Abc+def+ghixx ') results: [[0 11 0 11 0 8 0 4] [15 26 15 26 15 23 15 19]] Analysis: Reference find Submatchindex Testreg (' A (x*) b (y|z) c ', '-axxxbyc--abzc-') results: [[1 8 2 5 6 7] [11 15 12 12 13 14]] Analysis: Refer to Findsubmatchindex testreg (' a*r ', ' paranoabrmal ') results: [[[1 3] [8 9]] Analysis: reference fin Dsubmatchindex */REGP: = RegExp. Mustcompileposix (PAT) FMT. Printf ("%q\n", (REGP). Find (SRC))) fmt. Printf ("%q\n", (REGP). FindString (SRCSTR)) fmt. Printf ("%q\n", (REGP). FINDALL (SRC,-1))) FMT. Printf ("%q\n", (REGP). Findallstring (Srcstr,-1))) FMT. Printf ("%d\n", (REGP). FindIndex (SRC))) fmt. Printf ("%d\n", (REGP). Findstringindex (SRCSTR)) fmt. Printf ("%d\n", REGP. Findallindex (SRC,-1))) FMT. Printf ("%d\n", (REGP). Findallstringindex (Srcstr,-1))) FMT. PRINTLN ("Begin Submatch") fmt. Printf ("%q\n", (REGP). Findsubmatch (SRC))) fmt. Printf ("%q\n", (REGP). Findstringsubmatch (SRCSTR)) fmt. Printf ("%q\n", (REGP). Findallsubmatch (SRC,-1))) FMT. Printf ("%q\n", (REGP). Findallstringsubmatch (Srcstr,-1))) FMT. Printf ("%d\n", (REGP). Findsubmatchindex (SRC))) fmt. Printf ("%d\n", (REGP). Findstringsubmatchindex (SRCSTR)) fmt. Printf ("%d\n", (REGP). Findallsubmatchindex (SRC,-1))) FMT. Printf ("%d\n", (REGP). Findallstringsubmatchindex (Srcstr,-1)))}func main () {Testreg (' ((ABC.) def.) GHI) x* ', ' Abc-def-ghixxa Abc+def+ghixx ') Testreg (' ((ABC.) def.) GHI) ', ' Abc-def-ghixxa Abc+def+ghixx ') Testreg (' A (x*) b (y|z) c ', '-axxxbyc--abzc-') testreg (' A*r ', ' Paranoabrmal ')}