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
+5.5k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+10.7k Golang : Get currencies exchange rates example
+5.1k Golang : Customize scanner.Scanner to treat dash as part of identifier
+11.6k Golang : Simple file scaning and remove virus example
+7.4k Gogland : Single File versus Go Application Run Configurations
+18.7k Golang : Delete duplicate items from a slice/array
+11k Golang : Simple image viewer with Go-GTK
+7.1k Golang : Dealing with postal or zip code example
+9.8k Golang : Sort and reverse sort a slice of integers
+6.8k Fix sudo yum hang problem with no output or error messages
+10.6k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+5k Golang : Get a list of crosses(instruments) available to trade from Oanda account