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

  • 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.

主站蜘蛛池模板: 黑水县| 平乐县| 招远市| 平乐县| 内丘县| 黄平县| 荥阳市| 哈密市| 会昌县| 来宾市| 西青区| 宣恩县| 玉溪市| 二手房| 金坛市| 昭觉县| 维西| 读书| 高州市| 额尔古纳市| 上蔡县| 大渡口区| 东乌| 泸水县| 沙雅县| 元阳县| 彭阳县| 叶城县| 临城县| 贵德县| 本溪| 共和县| 连南| 丹巴县| 日照市| 永康市| 阿拉善右旗| 房产| 揭阳市| 介休市| 同仁县|