Friday, August 10, 2012

Simple RegExp in Go

package main

import (
  "fmt"
  "regexp"
)

func main() {
  re := regexp.MustCompile("/page/(?P[\\d]+)/")
  b := []byte("/page/1/")
  x := re.Find(b)
  fmt.Printf("Found: `%s`\n", x)
  x1 := re.FindAll(b, 100)
  fmt.Printf("Found: `%s`\n", x1)
  fmt.Printf("Subexp num: `%d`\n", re.NumSubexp())
  x2 := re.FindAllSubmatch(b, 100)
  fmt.Printf("Found: `%s`\n", x2)
  fmt.Printf("Subexp names: %s", re.SubexpNames())
}

And output:
Found: `/page/1/`
Found: `[/page/1/]`
Subexp num: `1`
Found: `[[/page/1/ 1]]`
Subexp names: [ pagenr]
You can try it here: http://play.golang.org/p/SqUe4X7vuR

1 comment: