Golang regexp.FindAllString() and FindString() functions example
package regexp
Golang regexp.FindAllString() and FindString() functions usage example
package main
import (
"fmt"
"regexp"
)
func main() {
// regular expression pattern
regE := regexp.MustCompile("/oid/([\\d]+)/")
searchString := "/oid/1/something_else/oid/2/"
matchSlice := regE.FindString(searchString)
fmt.Println("Found leftmost match : ", matchSlice)
// --- find all string match
matchAllSlice := regE.FindAllString(searchString, 500)
for k, v := range matchAllSlice {
// found /oid/1/ and /oid/2
// and return locations of the all matches
fmt.Printf("Found #%d match:[%v]\n", k, v)
}
}
output :
Found leftmost match : /oid/1/
Found #0 match:[/oid/1/]
Found #1 match:[/oid/2/]
References :
Advertisement
Something interesting
Tutorials
+13.6k Golang : Strings comparison
+6.2k Golang & Javascript : How to save cropped image to file on server
+29.4k Golang : JQuery AJAX post data to server and send data back to client example
+6.7k Golang : Humanize and Titleize functions
+46.2k Golang : Read tab delimited file with encoding/csv package
+11k Golang : Replace a parameter's value inside a configuration file example
+8.2k Android Studio : Rating bar example
+15.9k Golang : Update database with GORM example
+19.2k Golang : Check if directory exist and create if does not exist
+10.6k Golang : Resolve domain name to IP4 and IP6 addresses.
+11.3k Golang : How to use if, eq and print properly in html template