Golang : Regular Expression find string example
This is a simple tutorial on how to use Golang's regexp package to find certain string value in a string. Nothing fancy. Just writing this down for future reference.
package main
import (
"fmt"
"regexp"
)
func main() {
// regular expression pattern
regE := regexp.MustCompile("/oid/([\\d]+)/")
// simulate a search
// first convert string to byte for Find() function
searchByte := []byte("/oid/1/")
matchSlice := regE.Find(searchByte)
fmt.Printf("%s \n", matchSlice) // if found, return leftmost match, without 'abc'
matchSlice2 := regE.FindAll(searchByte, 500)
fmt.Printf("%s \n", matchSlice2) // if found, return all successive matches
oid := regE.NumSubexp()
fmt.Printf("OID is %d \n", oid)
// this is how to search by string
matchSlice3 := regE.FindAllString(string(searchByte), -1)
fmt.Printf("%s \n", matchSlice3) // if found, return all successive matches
}
See also : Golang : Regular Expression for alphanumeric and underscore
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+13.3k Golang : Image to ASCII art example
+7k Golang : File system scanning
+7.4k Gogland : Where to put source code files in package directory for rookie
+17.2k Golang : Linked list example
+7.5k Golang : Example of how to detect which type of script a word belongs to
+10.9k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+12.8k Golang : Convert(cast) uintptr to string example
+35.1k Golang : Integer is between a range
+14.2k Golang : Find network of an IP address
+16.8k Golang : XML to JSON example
+12.4k Golang : Listen and Serve on sub domain example
+3.4k Java : Get FX sentiment from website example