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

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.

主站蜘蛛池模板: 临安市| 繁昌县| 虎林市| 津南区| 海口市| 准格尔旗| 通渭县| 佛坪县| 家居| 芜湖市| 名山县| 双辽市| 汶川县| 子洲县| 荔浦县| 泗水县| 全南县| 浮山县| 邓州市| 灵武市| 辉南县| 泰顺县| 通许县| 新乐市| 达拉特旗| 广德县| 田林县| 上林县| 西乡县| 乐至县| 江陵县| 谢通门县| 梁河县| 泾阳县| 隆昌县| 淮滨县| 新余市| 慈溪市| 孝义市| 呼玛县| 三河市|