官术网_书友最值得收藏!

  • Go Systems Programming
  • Mihalis Tsoukalos
  • 557字
  • 2021-07-02 18:08:01

Pattern matching and regular expressions

Pattern matching, which plays a key role in Go, is a technique for searching a string for a set of characters based on a specific search pattern that is based on regular expressions. If pattern matching is successful, it allows you to extract the desired data from the string or replace or delete it. Grammar is a set of production rules for strings in a formal language. The production rules describe how to create strings from the alphabet of the language that are valid according to the syntax of the language. Grammar does not describe the meaning of a string or what can be done with it in whatever context, only its form. What is important is to realize that grammar is at the heart of regular expressions because without it, you cannot define or use a regular expression.

Regular expressions and pattern matching are not a panacea, so you should not try to solve every problem using regular expressions since they are not suitable for every kind of problem you may come up against. Furthermore, they might introduce unnecessary complexity to your software.

The Go package responsible for the pattern matching capabilities of Go is called regexp, which you can see in action in regExp.go. The code of regExp.go will be presented in four parts.

The first part is the expected preamble:

package main 
 
import ( 
   "fmt" 
   "regexp" 
) 

The second part is as follows:

func main() { 
match, _ := regexp.MatchString("Mihalis", "Mihalis Tsoukalos") 
   fmt.Println(match) 
   match, _ = regexp.MatchString("Tsoukalos", "Mihalis tsoukalos") 
   fmt.Println(match) 

Both calls to regexp.MatchString() try to find a static string, which is the first parameter, in a given string, which is the second parameter.

The third part contains a single, yet crucial, line of Go code:

   parse, err := regexp.Compile("[Mm]ihalis") 

The regexp.Compile() function reads the provided regular expression and tries to parse it. If the parsing of the regular expressing is successful, then regexp.Compile() returns a value of the regexp.Regexp variable type that you can use afterward. The [Mm] expression in the regexp.Compile() function means that what you are looking for can begin with an uppercase M or a lowercase m. Both [ and ] are special characters that are not part of the regular expression. So, the provided grammar is naive and only matches the words Mihalis and mihalis.

The last part uses the previous regular expression that is stored in the parse variable:

   if err != nil { 
         fmt.Printf("Error compiling RE: %s\n", err) 
   } else { 
         fmt.Println(parse.MatchString("Mihalis Tsoukalos")) 
         fmt.Println(parse.MatchString("mihalis Tsoukalos")) 
         fmt.Println(parse.MatchString("M ihalis Tsoukalos")) 
         fmt.Println(parse.ReplaceAllString("mihalis Mihalis", "MIHALIS")) 
   } 
} 

Running regExp.go generates the next output:

$ go run regExp.go
true
false
true
true
false
MIHALIS MIHALIS

So, the first call to regexp.MatchString() was a match, but the second was not because pattern matching is case-sensitive and Tsoukalos does not match tsoukalos. The parse.ReplaceAllString() function at the end searches the string that is given as an input ("mihalis Mihalis") and replaces each match with the string that is given as its second parameter ("MIHALIS").

The rest of this section will present various examples using static text because you do not know how to read text files yet. However, as the static text will be stored in an array and processed line by line, the presented code can be easily modified to support getting your input from external text files.

主站蜘蛛池模板: 三台县| 冕宁县| 峨眉山市| 达拉特旗| 曲麻莱县| 会宁县| 忻州市| 锡林浩特市| 江源县| 临夏市| 黔西县| 五华县| 凤阳县| 石景山区| 江达县| 古浪县| 马鞍山市| 景洪市| 海丰县| 壶关县| 治多县| 乌兰察布市| 甘肃省| 石狮市| 湾仔区| 辰溪县| 宣化县| 松滋市| 凤山市| 漯河市| 莫力| 万荣县| 安图县| 常德市| 永年县| 虹口区| 西平县| 高要市| 临夏市| 廉江市| 姜堰市|