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