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
+12.7k Swift : Convert (cast) Int to String ?
+77.7k Golang : How to return HTTP status code?
+7.9k Golang : Routes multiplexer routing example with regular expression control
+13.2k Golang : Qt progress dialog example
+13.7k Golang : concatenate(combine) strings
+10k Golang : cannot assign type int to value (type uint8) in range error
+4.4k JavaScript : Rounding number to decimal formats to display currency
+13.5k Golang : Gin framework accept query string by post request example
+28.1k Golang : Change a file last modified date and time
+15.2k Golang : How to convert(cast) IP address to string?
+17.5k Golang : Login and logout a user after password verification and redirect example
+8.3k Golang : Another camera capture GUI application with GTK and OpenCV