Golang Regular Expression Match: Regexp Syntax Tutorial Examples

Golang Regular Expression Search Examples


Golang Regular ExpressionThe regexp package implements golang regular expression search and pattern matching. The syntax is accepted by RE2 and is the same used by other popular languages, like Perl or Python.

Regexp is an excellent standard Go package that can be used for basic to advance pattern matching. It makes complex patterns work faster since its guaranteed to run in linear time. For an overview of the syntax run go doc regexp/syntaxor reference the google github re2 wiki for a list of regular expression syntax accepted by RE2.

Overview

Common Match Patterns

Below are common match patterns; a full set can be found here.

Expression Meaning
. Any character
* Any number of unknown characters (x* would be zero or more x)
+ One or more occurrences (x+ would be one or more x)
{3} Exactly three of previous
{3,5} Between three and five of previous
^ At beginning of text or line – line if (?m) is used
$ At end of text or line – line if (?m) is used
| Logical OR
\ Escape special character
\d A digit 0-9
\D A non-digit 0-9
\s A whitespace character
\S A non-whitespace character
\w A word character: [0-9A-Za-z_]
\W A non-word character
\n A newline
\t A tab
[abc] Character a or b or c
[^abc] Any character except a or b or c
[a-z] Any character from a to z
[a-z0-9] Any character from a to z or 0 to 9

Common Regexp Methods

 

Regexp Method Purpose
Expand appends a template to a byte slice (template and source are byte[])
ExpandString appends a template to a byte slice (template & source are string)
FindString returns a string after the first match
FindStringIndex returns a two entry array – first entry is the index (starting from 0) where the regular expression matched – the second is the index in front of which the regexp ended (if it ended at index 5, it returns 6)
FindStringSubmatch return slice of whole-pattern match and the submatch within those matches
FindAllString accepts match and max number of matches, if negative (-1) then return all matches – returns a slice of all successive matches
FindAllStringIndex “all” version of FindStringIndex – returns a 2-d array containing two entry array’s for all matches
FindAllStringSubmatch “all” version of FindStringSubmatch – returns a 2-d array
FindAllStringSubmatchIndex “all” version of FindStringSubmatchIndex – returns a 2-d array
MatchString reports if the string is matched
ReplaceAllLiteralString returns a copy of original input, replacing matches of the regex with the replacement string – example, allows you to interpret the dollar sign ($) literally
ReplaceAllString returns a copy of original input, replacing matches of the regex with the replacement string
Split slice string into substrings separated by the expression and return a slice of substrings between those expression matches – int determines the number of substrings to return (n > 0 for most; n=0 for nil; n<0 for all substrings)
SubexpNames returns the names of the parenthesized subexpression regex patterns

 

Regexp.Expand

appends a template to a byte slice (template and source are byte[])

func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte

Example:

Try it out in the Go Playground.

 

Regexp.ExpandString

appends a template to a byte slice (template and source are string)

func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte

Example:

Try it out in the Go Playground.

 

Regexp.FindString

returns a string after the first match

func (re *Regexp) FindString(s string) string

Example:

Try it out in the Go Playground.

 

Regexp.FindStringIndex

returns a two entry array – first entry is the index (starting from 0) where the regular expression matched – the second is the index in front of which the regexp ended (if it ended at index 5, it returns 6)

func (re *Regexp) FindStringIndex(s string) (loc []int)

Example:

Try it out in the Go Playground.

 

Regexp.FindStringSubmatch

return slice of whole-pattern match and the submatch within those matches

func (re *Regexp) FindStringSubmatch(s string) []string

Example:

Try it out in the Go Playground.

 

Regexp.FindAllString

accepts match and max number of matches, if negative (-1) then return all matches – returns a slice of all successive matches

func (re *Regexp) FindAllString(s string, n int) []string

Example:

Try it out in the Go Playground.

 

Regexp.FindAllStringIndex

“all” version of FindStringIndex – returns a 2-d array containing two entry array’s for all matches

func (re *Regexp) FindAllStringIndex(s string, n int) [][]int

Try it out in the Go Playground.

 

Regexp.FindAllStringSubmatch

“all” version of FindStringSubmatch – returns a 2-d array

func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string

Example:

Try it out in the Go Playground.

 

Regexp.FindAllStringSubmatchIndex

“all” version of FindStringSubmatchIndex – returns a 2-d array

func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int

Example:

Try it out in the Go Playground.

 

Regexp.MatchString

reports if the string is matched

func (re *Regexp) MatchString(s string) bool

Example:

Try it out in the Go Playground.

 

Regexp.ReplaceAllLiteralString

returns a copy of original input, replacing matches of the regex with the replacement string – example, allows you to interpret the dollar sign ($) literally

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

Example:

Try it out in the Go Playground.

 

Regexp.ReplaceAllString

returns a copy of original input, replacing matches of the regex with the replacement string

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

Example:

Try it out in the Go Playground.

 

Regexp.Split

slice string into substrings separated by the expression and return a slice of substrings between those expression matches – int determines the number of substrings to return (n > 0 for most; n=0 for nil; n<0 for all substrings)

func (re *Regexp) Split(s string, n int) []string

Example single-line:

Try it out in the Go Playground.

Example multi-line: Useful to split Cisco or Arista style configuration blocks into individual slices for later processing.

 

Regexp.SubexpNames

returns the names of the parenthesized subexpression regex patterns – index 0 is always an empty string

func (re *Regexp) SubexpNames() []string

Examples:

Try it out in the Go Playground.

 

Try it out in the Go Playground.

 

See also:
Golang SSH Client
Golang DNS Lookup
Golang Binary Only Package
Golang IP Address Manipulation