rxscan provides functionality to scan text to variables using regular expression capture group.

rxscan

rxscan provides functionality to scan text to variables using regular expression capture group.

This library is still experimental, use at your own risk. Contributions are always welcome and please submit an issue if you find any problem.

Examples

Scanning a string

	input := "bright white bags contain 9 shiny gold bag."
	rx := regexp.MustCompile(`([\w ]+) bags contain (\d+) ([\w ]+) bag.`)
	var (
		bag1, bag2 string
		i          int
	)
	n, err := rxscan.Scan(rx, input, &bag1, &i, &bag2)
	if err != nil {
		panic(err)
	}

	fmt.Printf("parsed %d arguments: %s -> (%d) %s", n, bag1, i, bag2)

	// Output: parsed 3 arguments: bright white -> (9) shiny gold

Scanning repeated pattern

	input := `light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
faded blue bags contain no other bags.
dotted black bags contain no other bags.`

	rx := regexp.MustCompile(`(\d+) ([\w ]+) bag`)

	sc := rxscan.NewScanner(rx, input)
	for sc.More() {
		var count int
		var color string
		_, err := sc.Scan(&count, &color)
		if err != nil {
			panic(err)
		}

		fmt.Printf("- (%d) %s\n", count, color)
	}
	if err := sc.Error(); err != nil {
		panic(err)
	}

	// Output:
	//- (1) bright white
	//- (2) muted yellow
	//- (3) bright white
	//- (4) muted yellow
	//- (1) shiny gold
	//- (2) shiny gold
	//- (9) faded blue
	//- (1) dark olive
	//- (2) vibrant plum
	//- (3) faded blue
	//- (4) dotted black
	//- (5) faded blue
	//- (6) dotted black
Similar Resources

Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite

Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite

go-testdeep Extremely flexible golang deep comparison, extends the go testing package. Latest news Synopsis Description Installation Functions Availab

Jan 5, 2023

Hotswap provides a solution for reloading your go code without restarting your server, interrupting or blocking any ongoing procedure.

Hotswap provides a solution for reloading your go code without restarting your server, interrupting or blocking any ongoing procedure.

Hotswap provides a solution for reloading your go code without restarting your server, interrupting or blocking any ongoing procedure. Hotswap is built upon the plugin mechanism.

Jan 5, 2023

keeper is package for Go that provides a mechanism for waiting a result of execution function until context cancel.

keeper is package for Go that provides a mechanism for waiting a result of execution function until context cancel.

Apr 18, 2022

Package truthy provides truthy condition testing with Go generics

Package truthy provides truthy condition testing with Go generics

Truthy Truthy is a package which uses generics (Go 1.18+) to create useful boolean tests and helper functions. Examples // truthy.Value returns the tr

Nov 11, 2022

golden provides utilities for golden file tests.

golden provides utilities for golden file tests.

Dec 27, 2022

This library provides an ASTERIX Frame(binary data) decoding/parsing(json,xml) capabilities for Go.

GoAsterix This library provides an ASTERIX Frame(binary data) decoding/parsing(json,xml) capabilities for Go. ASTERIX ASTERIX (All Purpose Structured

Dec 13, 2022

A library that provides Go Generics friendly "optional" features.

go-optional A library that provides Go Generics friendly "optional" features. Synopsis some := optional.Some[int](123) fmt.Printf("%v\n", some.IsSome(

Dec 20, 2022

Provides simple, semantic manipulation of the operating system's signal processing.

Provides simple, semantic manipulation of the operating system's signal processing.

Provides simple, semantic manipulation of the operating system's signal processing.

Dec 15, 2021

Slice - provides generic Map, Reduce and Filter functions for Go.

slice slice is a simple Go package to provide generic versions of Map, Reduce and Filter on slices. I mainly wrote it as an exercise to get more famil

Jan 1, 2023
Related tags
Generates random text based on trigrams generated from input text
Generates random text based on trigrams generated from input text

Trigrams Generates random text based on trigrams generated from input text Contents Building Running Using Implementation notes NGram size Maximum wor

Feb 9, 2022
Go library that provides fuzzy string matching optimized for filenames and code symbols in the style of Sublime Text, VSCode, IntelliJ IDEA et al.
Go library that provides fuzzy string matching optimized for filenames and code symbols in the style of Sublime Text, VSCode, IntelliJ IDEA et al.

Go library that provides fuzzy string matching optimized for filenames and code symbols in the style of Sublime Text, VSCode, IntelliJ IDEA et al. This library is external dependency-free. It only depends on the Go standard library.

Dec 27, 2022
a decision & trigger framework backed by Google's Common Expression Language used in graphikDB

a decision & trigger framework backed by Google's Common Expression Language used in graphikDB

Nov 15, 2022
Simple expression evaluation engine for Go

??️ chili Currently in development, Unstable (API may change in future) Simple expression evaluation engine. Expression is one liner that evalutes int

Nov 8, 2022
go expression

golang 实现的表达式计算 功能清单 四则运算 +-*/ 实现; 逻辑运算 and or && || 实现; 参数传递; 逻辑运算符 > >= < <= ! 实现; 自定义方法注入; TODO 优雅的错误返回; 自定义方法注入; 使用 go get -u github.com/huangxing

Sep 29, 2021
This project provides some working examples using Go and Hotwire Turbo.

hotwire-golang-website This project provides some working examples using Go the hotwire/turbo library published by basecamp.

Dec 29, 2022
Go-path - A helper package that provides utilities for parsing and using ipfs paths

go-path is a helper package that provides utilities for parsing and using ipfs paths

Jan 18, 2022
📋 cross-platform clipboard package that supports accessing text and image in Go (macOS/Linux/Windows/Android/iOS)

clipboard Cross platform (macOS/Linux/Windows/Android/iOS) clipboard package in Go import "golang.design/x/clipboard" Features Cross platform supports

Dec 24, 2022
Simple utilities for creating ascii text in Go

Simple utilities for creating ascii text in Go

Oct 30, 2021
tail a text file and trigger an action if a new line occurs [wip]

Tailpipe Synopsis Config Help Synopsis Tail a file and trigger an action if a new line occurs. Currently only mailing the new line somewhere is suppor

Dec 23, 2021