January 2019
Beginner
132 pages
3h 14m
English
Regular expressions can also be used to find content displayed on the web page itself. For example, you may be trying to find the price of an item. Let's look at the following example that shows the price of the Hands-On Go Programming book from Packt Publishing's website:
package mainimport ( "fmt" "io/ioutil" "net/http" "regexp")func main() { resp, err := http.Get("https://www.packtpub.com/application-development/hands-go-programming") if err != nil { panic(err) } data, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } stringBody := string(data) re := regexp.MustCompile(`.*main-book-price.*\n.*(\$[0-9]*\.[0-9]{0,2})`) priceMatches := re.FindStringSubmatch(stringBody) fmt.Printf("Book Price: %s\n", priceMatches[1]) ...Read now
Unlock full access