Go Package regexp

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

import (
	"regexp"
)

var someRegexp *regexp.Regexp
someRegexp = regexp.MustCompile("(\\D+)(\\d+),(\\d)")

groups := someRegexp.FindSubmatch([]byte(s))
if groups == nil {
  // no match 
  ...
}

entireString := groups[0]
firstGroup := groups[1]
secondGroup := groups[2]
thirdGroup := groups[3]

Functions

Matching

regexp.MatchString()

var regex = regexp.MustCompile("^[_a-zA-Z][_a-zA-Z0-9]*$")
[...]
if regex.MatchString(s) {
  ...
}

Replacement

Needs a Regexp instance.

regexp.ReplaceAllString()

func (re *Regexp) ReplaceAllString(src, repl string) string

There is a strings.Replace(...) function, but it does not take regular expressions.

Groups

TODO