Golang Regular Expression Tutorial

GolangGolangBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab demonstrates how to use regular expressions in Golang.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/AdvancedTopicsGroup(["`Advanced Topics`"]) go/AdvancedTopicsGroup -.-> go/regular_expressions("`Regular Expressions`") subgraph Lab Skills go/regular_expressions -.-> lab-15502{{"`Golang Regular Expression Tutorial`"}} end

Regular Expressions

The lab requires you to complete the code to perform various regular expression-related tasks in Golang.

  • Use the regexp package to perform regular expression-related tasks.
  • Use MatchString to test whether a pattern matches a string.
  • Use Compile to optimize a Regexp struct.
  • Use MatchString to test a match like Compile.
  • Use FindString to find the match for the regexp.
  • Use FindStringIndex to find the first match and return the start and end indexes for the match instead of the matching text.
  • Use FindStringSubmatch to return information for both p([a-z]+)ch and ([a-z]+).
  • Use FindStringSubmatchIndex to return information about the indexes of matches and submatches.
  • Use FindAllString to find all matches for a regexp.
  • Use FindAllStringSubmatchIndex to apply to all matches in the input, not just the first.
  • Use Match to test a match with []byte arguments and drop String from the function name.
  • Use MustCompile to create global variables with regular expressions.
  • Use ReplaceAllString to replace subsets of strings with other values.
  • Use ReplaceAllFunc to transform matched text with a given function.
$ go run regular-expressions.go
true
true
peach
idx: [0 5]
[peach ea]
[0 5 1 3]
[peach punch pinch]
all: [[0 5 1 3] [6 11 7 9] [12 17 13 15]]
[peach punch]
true
regexp: p([a-z]+)ch
a <fruit>
a PEACH

## For a complete reference on Go regular expressions check
## the [`regexp`](https://pkg.go.dev/regexp) package docs.

There is the full code below:

// Go offers built-in support for [regular expressions](https://en.wikipedia.org/wiki/Regular_expression).
// Here are some examples of  common regexp-related tasks
// in Go.

package main

import (
	"bytes"
	"fmt"
	"regexp"
)

func main() {

	// This tests whether a pattern matches a string.
	match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
	fmt.Println(match)

	// Above we used a string pattern directly, but for
	// other regexp tasks you'll need to `Compile` an
	// optimized `Regexp` struct.
	r, _ := regexp.Compile("p([a-z]+)ch")

	// Many methods are available on these structs. Here's
	// a match test like we saw earlier.
	fmt.Println(r.MatchString("peach"))

	// This finds the match for the regexp.
	fmt.Println(r.FindString("peach punch"))

	// This also finds the first match but returns the
	// start and end indexes for the match instead of the
	// matching text.
	fmt.Println("idx:", r.FindStringIndex("peach punch"))

	// The `Submatch` variants include information about
	// both the whole-pattern matches and the submatches
	// within those matches. For example this will return
	// information for both `p([a-z]+)ch` and `([a-z]+)`.
	fmt.Println(r.FindStringSubmatch("peach punch"))

	// Similarly this will return information about the
	// indexes of matches and submatches.
	fmt.Println(r.FindStringSubmatchIndex("peach punch"))

	// The `All` variants of these functions apply to all
	// matches in the input, not just the first. For
	// example to find all matches for a regexp.
	fmt.Println(r.FindAllString("peach punch pinch", -1))

	// These `All` variants are available for the other
	// functions we saw above as well.
	fmt.Println("all:", r.FindAllStringSubmatchIndex(
		"peach punch pinch", -1))

	// Providing a non-negative integer as the second
	// argument to these functions will limit the number
	// of matches.
	fmt.Println(r.FindAllString("peach punch pinch", 2))

	// Our examples above had string arguments and used
	// names like `MatchString`. We can also provide
	// `[]byte` arguments and drop `String` from the
	// function name.
	fmt.Println(r.Match([]byte("peach")))

	// When creating global variables with regular
	// expressions you can use the `MustCompile` variation
	// of `Compile`. `MustCompile` panics instead of
	// returning an error, which makes it safer to use for
	// global variables.
	r = regexp.MustCompile("p([a-z]+)ch")
	fmt.Println("regexp:", r)

	// The `regexp` package can also be used to replace
	// subsets of strings with other values.
	fmt.Println(r.ReplaceAllString("a peach", "<fruit>"))

	// The `Func` variant allows you to transform matched
	// text with a given function.
	in := []byte("a peach")
	out := r.ReplaceAllFunc(in, bytes.ToUpper)
	fmt.Println(string(out))
}

Summary

This lab demonstrates how to use regular expressions in Golang to perform various tasks such as testing whether a pattern matches a string, finding the match for the regexp, and replacing subsets of strings with other values.

Other Golang Tutorials you may like