Golang regexp.FindAll() function example

package regexp

Golang regexp.FindAll() function usage example

 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/something_else/oid/2/")

  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
 }

Output :

/oid/1/

[/oid/1/ /oid/2/]

Reference :

http://golang.org/pkg/regexp/#Regexp.FindAll

Advertisement