This is a creation in Article, where the information may have evolved or changed.
The regexp package is used in the go language to use regular calibration
Let's introduce several common methods:
1. using the matchstring function
Regexp. Matchstring (Pattern string, s string) pattern is a regular expression, S is the nginx example of a character that needs to be verified: Match,_:=regexp. The first parameter returned by matchstring ("P ([a-z]+) ch", "Peddach") is the bool type that matches the result, and the second parameter is the error type FMT. PRINTLN (Match)//result is true2, using compile function or Mustcompile function their difference is compile return two parameter *regexp,error type, and Mustcompile only return * Regexp type func Compile (expr string) (*regexp, error) {}func mustcompile (str string) *regexp {} Their role is to compile the regular expression and return the optimized REGEXP structure, the structure of the body has a number of methods required. Example R,_:=regexp.compile ("P ([a-z]+) ch") b:=r.matchstring ("Peach") fmt. PRINTLN (b)//result is true or r1:=regexp. Mustcompile ("P ([a-z]+) ch") B1:r1. Matchstring ("Peach") fmt. PRINTLN (b)//results for some commonly used methods in TRUE3 and regexp structures R,_:=regexp.compile ("p ([a-z]+) ch") Fmt. Println (r.matchstring ("Peach"))//j Result: true//Find matching string fmt. Println (r.findstring ("Peach Punch"))//Print Result: Peach//Find the index that matches the starting and ending positions of the string instead of matching the content [0 5] FMT. Println (R.findstringindex ("Peach Punch"))//print Result: [0 5]//return exact match and local match string, for example, this will return the information of P ([a-z]+) ch and ' ([a-z]+)] Fmt. Println (r.findstrIngsubmatch ("Peach Punch"))//Print result: [Peach EA]//return exact match and local match index position FMT. Println (R.findstringsubmatchindex ("Peach Punch"))//Printing results: [0 5 1 3]//Returns all occurrences, not just the first match. Positive integers are used to limit the number of matches to FMT. Println (r.findallstring ("Peach Punch Pinch",-1))//Print result: [Peach punch pinch] fmt. Println (r.findallstring ("Peach Punch Pinch", 2))//Match two times print result: [Peach punch]//Returns all the exact matches and local matches of the index position of FMT. Println (R.findallstringsubmatchindex ("Peach Punch Pinch",-1))//Print results: [[0 5 1 3] [6 11 7 9] [12 17 13 15]]//The above example, We used the string as a parameter and used a method such as matchstring. We can also provide the []byte parameter and remove the String from the function hit. Fmt. Println (R.match ([]byte ("Peach"))//Print Result: true r1:=regexp. Mustcompile ("P ([a-z]+) ch")//will match the results, replacing the new input results with FMT. Println (R1. Replaceallstring ("A Peach", "<fruit>")//Print Result: A <fruit>//func variable allows the matching content to be passed into a given function, In:=[]byte ("a Peach") out:=r1. Replaceallfunc (in,bytes. ToUpper) fmt. Printf (String (out))//Print Result: a PEACH