Match regex group into go struct using struct tags and automatic parsing

Overview

regroup

Simple library to match regex expression named groups into go struct using struct tags and automatic parsing

codecov License PkgGoDev Go Report Card codeclimate

Installing

go get github.com/oriser/regroup

Example

Named groups map

package main

import (
	"fmt"
	"github.com/oriser/regroup"
)

var re = regroup.MustCompile(`(?P<duration>.*?)\s+(?P<num>\d+)\s+(?P<foo>.*)`)

func main() {
	mathces, err := re.Groups("5s 123 bar")
	if err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", mathces)
}

Will output: map[duration:5s foo:bar num:123]

Single match

package main

import (
	"fmt"
	"github.com/oriser/regroup"
	"time"
)

var re = regroup.MustCompile(`(?P<duration>.*?)\s+(?P<num>\d+)\s+(?P<foo>.*)`)

type B struct {
	Str string `regroup:"foo"`
}

type A struct {
	Number        int           `regroup:"num"`
	Dur           time.Duration `regroup:"duration"`
	AnotherStruct B
}

func main() {
	a := &A{}
	if err := re.MatchToTarget("5s 123 bar", a); err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", a)
}

Will output: &{Number:123 Dur:5s AnotherStruct:{Str:bar}}

Multiple matches

You can also get all matches parsed as given target struct. The return value in this case will be an array of interfaces, you should cast it to the target type in order to access its fields.

package main

import (
	"fmt"
	"github.com/oriser/regroup"
	"time"
)

var re = regroup.MustCompile(`\s*(?P<duration>.*?)\s+(?P<num>\d+)\s+(?P<foo>.*)`)

type B struct {
	Str string `regroup:"foo"`
}

type A struct {
	Number        int           `regroup:"num"`
	Dur           time.Duration `regroup:"duration"`
	AnotherStruct B
}

func main() {
	a := &A{}
	s := `5s 123 bar1
		  1m 456 bar2
		  10h 789 bar3`
	rets, err := re.MatchAllToTarget(s, -1, a)
	if err != nil {
		panic(err)
	}
	for _, elem := range rets {
		fmt.Printf("%+v\n", elem.(*A))
	}
}

Will output:

&{Number:123 Dur:5s AnotherStruct:{Str:bar1}}
&{Number:456 Dur:1m0s AnotherStruct:{Str:bar2}}
&{Number:789 Dur:10h0m0s AnotherStruct:{Str:bar3}}

Required groups

You can specify that a specific group is required, means that it can't be empty.

If a required group is empty, an error (*regroup.RequiredGroupIsEmpty) will be returned .

package main

import (
	"fmt"
	"github.com/oriser/regroup"
	"time"
)

var re = regroup.MustCompile(`(?P<duration>.*?)\s+(?P<num>\d+)\s+(?P<foo>.*)`)

type B struct {
	Str string `regroup:"foo,required"`
}

type A struct {
	Number        int           `regroup:"num"`
	Dur           time.Duration `regroup:"duration"`
	AnotherStruct B
}

func main() {
	a := &A{}
	if err := re.MatchToTarget("5s 123 ", a); err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", a)
}

Will return an error: required regroup "foo" is empty for field "Str"

Supported struct field types

  • time.Duration
  • bool
  • string
  • int
  • int8
  • int16
  • int32
  • int64
  • uint
  • uint8
  • uint16
  • uint32
  • uint64
  • float32
  • float64

Pointers and nested structs are also supported, both on single match and multiple matches

You might also like...
A fast, easy-of-use and dependency free custom mapping from .csv data into Golang structs

csvparser This package provides a fast and easy-of-use custom mapping from .csv data into Golang structs. Index Pre-requisites Installation Examples C

A declarative struct-tag-based HTML unmarshaling or scraping package for Go built on top of the goquery library

goq Example import ( "log" "net/http" "astuart.co/goq" ) // Structured representation for github file name table type example struct { Title str

Preventing 3rd Party DLLs from Injecting into your Malware

Doge-BlockDLLs Preventing 3rd Party DLLs from Injecting into your Malware ACG(Arbitrary Code Guard)的方式等大佬来实现 Ref https://www.ired.team/offensive-secur

network .md into .html with plaintext files
network .md into .html with plaintext files

plain network markdown files into html with plaintext files plain is a static-site generator operating on plaintext files containing a small set of co

Turns any junk text into a usable wordlist for brute-forcing.

haklistgen Turns any junk text into a usable wordlist for brute-forcing. Installation go get -u github.com/hakluke/haklistgen Usage Examples Scrape a

golang program that simpily converts html into markdown

Simpily converts html to markdown Just a simple project I wrote in golang to convert html to markdown, surprisingly works decent for a lot of websites

Build "Dictionary of the Old Norwegian Language" into easier-to-use data formats

Old Norwegian Dictionary Builder Build "Dictionary of the Old Norwegian Language" into easier-to-use data formats. Available formats: JSON DSL XML Usa

Script to inject Aliucord into a Discord ipa.

Aliucord-Patcher Script to patch Discord's ipa with a custom build of hermes, apply a custom icon and apply changes to Info.plist. Usage: go run cmds/

Take screenshots of websites and create PDF from HTML pages using chromium and docker

gochro is a small docker image with chromium installed and a golang based webserver to interact wit it. It can be used to take screenshots of w

Comments
  • Complex group regex don't always work in MatchToTarget

    Complex group regex don't always work in MatchToTarget

    Describe the bug Nothing gets matched to the provided struct in MatchToTarget even though the regex does match

    To Reproduce

    package main
    
    import (
    	"fmt"
    	"github.com/oriser/regroup"
    )
    
    const stepNameRegexp = `^(?P<PictureURL>.*/system/(?P<Mentor>.*?)/(.*/)?(?P<Frame>.*)):(?P<Gershon>.*)$`
    
    type stepHandler struct {
    	vendor   string `regroup:"Vendor"`
    	name     string `regroup:"Name"`
    	imageURL string `regroup:"ImageURL"`
    	version  string `regroup:"Version"`
    }
    
    func createStepHandlerFromProto(fullStepName string) (*stepHandler, error) {
    	r := regroup.MustCompile(stepNameRegexp)
    	handler := &stepHandler{}
    
    	err := r.MatchToTarget(fullStepName, handler)
    	if err != nil {
    		fmt.Println(err)
    	}
    	return handler, nil
    }
    
    func main() {
    	h, err := createStepHandlerFromProto("i.am.robot/smackplus/system/magic/message/funk:1.1.15")
    	fmt.Printf("%#v, %s", h, err)
    }
    

    Expected behavior Struct should be filled with the data from the string

    opened by igorkheif 1
Releases(v0.1)
Owner
Ori Seri
Ori Seri
Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and struct tags for golang crawler

Pagser Pagser inspired by page parser。 Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and str

foolin 72 Nov 14, 2022
ByNom is a Go package for parsing byte sequences, suitable for parsing text and binary data

ByNom is a Go package for parsing byte sequences. Its goal is to provide tools to build safe byte parsers without compromising the speed or memo

Andrew Bashkatov 4 May 5, 2021
Super Fast Regex in Go

Rubex : Super Fast Regexp for Go by Zhigang Chen ([email protected] or [email protected]) ONLY USE go1 BRANCH A simple regular expression libr

Moovweb 218 Sep 9, 2022
A full-featured regex engine in pure Go based on the .NET engine

regexp2 - full featured regular expressions for Go Regexp2 is a feature-rich RegExp engine for Go. It doesn't have constant time guarantees like the b

Doug Clark 641 Nov 24, 2022
Diff, match and patch text in Go

go-diff go-diff offers algorithms to perform operations required for synchronizing plain text: Compare two texts and return their differences. Perform

Sergi Mansilla 1.4k Dec 2, 2022
Watches container registries for new and changed tags and creates an RSS feed for detected changes.

Tagwatch Watches container registries for new and changed tags and creates an RSS feed for detected changes. Configuration Tagwatch is configured thro

Wolfgang Popp 1 Jan 7, 2022
Produces a set of tags from given source. Source can be either an HTML page, Markdown document or a plain text. Supports English, Russian, Chinese, Hindi, Spanish, Arabic, Japanese, German, Hebrew, French and Korean languages.

Tagify Gets STDIN, file or HTTP address as an input and returns a list of most popular words ordered by popularity as an output. More info about what

ZoomIO 24 Sep 27, 2022
Path parsing for segment unmarshaling and slicing.

parth go get github.com/codemodus/parth/v2 Package parth provides path parsing for segment unmarshaling and slicing. In other words, parth provides s

Code Modus 42 Sep 27, 2022
Convert xml and json to go struct

xj2go The goal is to convert xml or json file to go struct file. Usage Download and install it: $ go get -u -v github.com/wk30/xj2go/cmd/... $ xj [-t

null 29 Oct 23, 2022
Takes a full name and splits it into individual name parts

gonameparts gonameparts splits a human name into individual parts. This is useful when dealing with external data sources that provide names as a sing

James Polera 38 Sep 27, 2022