Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Regular Expressions

This Go code demonstrates the usage of the regexp package for regular expression matching and manipulation. Let's go through the code with inline comments and explanations:

// Importing necessary packages.
import (
	"bytes"
	"fmt"
	"regexp"
)

// The main function, where the execution of the program begins.
func main() {
	// Using MatchString to check if a string matches a regular expression pattern.
	match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
	fmt.Println(match)

	// Compiling a regular expression pattern for reuse.
	r, _ := regexp.Compile("p([a-z]+)ch")

	// Using MatchString with the compiled regex.
	fmt.Println(r.MatchString("peach"))

	// Finding the first match in the input string.
	fmt.Println(r.FindString("peach punch"))

	// Finding the start and end indices of the first match.
	fmt.Println("idx:", r.FindStringIndex("peach punch"))

	// Finding submatches of the first match.
	fmt.Println(r.FindStringSubmatch("peach punch"))

	// Finding start and end indices of submatches of the first match.
	fmt.Println(r.FindStringSubmatchIndex("peach punch"))

	// Finding all matches in the input string.
	fmt.Println(r.FindAllString("peach punch pinch", -1))

	// Finding all start and end indices of submatches in all matches.
	fmt.Println("all:", r.FindAllStringSubmatchIndex(
		"peach punch pinch", -1))

	// Finding a specific number of matches.
	fmt.Println(r.FindAllString("peach punch pinch", 2))

	// Matching using a byte slice.
	fmt.Println(r.Match([]byte("peach")))

	// Compiling a regular expression using MustCompile for simplicity.
	r = regexp.MustCompile("p([a-z]+)ch")
	fmt.Println("regexp:", r)

	// Replacing all matches with a specified string.
	fmt.Println(r.ReplaceAllString("a peach", "<fruit>"))

	// Replacing all matches using a function.
	in := []byte("a peach")
	out := r.ReplaceAllFunc(in, bytes.ToUpper)
	fmt.Println(string(out))
}

Output

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

Explanation:

  1. MatchString and Compile:

    • MatchString checks if a string matches a regular expression pattern.

    • Compile is used to compile a regular expression pattern for later use.

  2. Finding Matches:

    • FindString finds the first match in the input string.

    • FindStringIndex finds the start and end indices of the first match.

    • FindStringSubmatch finds submatches of the first match.

    • FindStringSubmatchIndex finds start and end indices of submatches of the first match.

    • FindAllString finds all matches in the input string.

    • FindAllStringSubmatchIndex finds all start and end indices of submatches in all matches.

    • FindAllString can be used to find a specific number of matches.

  3. Matching and Replacing:

    • Match is used to match using a byte slice.

    • MustCompile is used to simplify regular expression compilation.

    • ReplaceAllString replaces all matches with a specified string.

    • ReplaceAllFunc replaces all matches using a function (bytes.ToUpper in this case).

These examples demonstrate common operations with regular expressions using the regexp package in Go. Regular expressions provide powerful and flexible pattern matching capabilities.

PreviousText TemplatesNextJSON

Last updated 1 year ago

Was this helpful?