{"id":13413980,"url":"https://github.com/oriser/regroup","last_synced_at":"2025-03-14T20:30:56.426Z","repository":{"id":39339303,"uuid":"293902653","full_name":"oriser/regroup","owner":"oriser","description":"Match regex group into go struct using struct tags and automatic parsing","archived":false,"fork":false,"pushed_at":"2024-06-10T20:05:00.000Z","size":38,"stargazers_count":143,"open_issues_count":3,"forks_count":13,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-07-31T20:53:14.448Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oriser.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-08T19:04:42.000Z","updated_at":"2024-06-10T20:10:44.000Z","dependencies_parsed_at":"2022-07-16T10:01:20.029Z","dependency_job_id":"88cd0eb6-007d-40e7-abb1-f6fcdb46594a","html_url":"https://github.com/oriser/regroup","commit_stats":{"total_commits":46,"total_committers":5,"mean_commits":9.2,"dds":"0.15217391304347827","last_synced_commit":"1b00c9bdbc5b687f820e1a8b5c0d1313ee4ab4ba"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oriser%2Fregroup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oriser%2Fregroup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oriser%2Fregroup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oriser%2Fregroup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oriser","download_url":"https://codeload.github.com/oriser/regroup/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243642046,"owners_count":20323951,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T20:01:54.311Z","updated_at":"2025-03-14T20:30:55.981Z","avatar_url":"https://github.com/oriser.png","language":"Go","readme":"# regroup\nSimple library to match regex expression named groups into go struct using struct tags and automatic parsing\n\n![](https://github.com/oriser/regroup/workflows/reviewdog/badge.svg)\n![](https://github.com/oriser/regroup/workflows/Go/badge.svg)\n[![codecov](https://codecov.io/gh/oriser/regroup/branch/master/graph/badge.svg)](https://codecov.io/gh/oriser/regroup)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/oriser/regroup)](https://pkg.go.dev/github.com/oriser/regroup)\n[![Go Report Card](https://goreportcard.com/badge/github.com/oriser/regroup?a=b)](https://goreportcard.com/report/github.com/oriser/regroup)\n[![codeclimate](https://api.codeclimate.com/v1/badges/169ebfa87cb6af0c6db6/maintainability)](https://goreportcard.com/report/github.com/oriser/regroup)\n\n### Installing\n`go get github.com/oriser/regroup`\n\n\n## Example\n#### Named groups map\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/oriser/regroup\"\n)\n\nvar re = regroup.MustCompile(`(?P\u003cduration\u003e.*?)\\s+(?P\u003cnum\u003e\\d+)\\s+(?P\u003cfoo\u003e.*)`)\n\nfunc main() {\n\tmatches, err := re.Groups(\"5s 123 bar\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Printf(\"%+v\\n\", matches)\n}\n```\nWill output:\n`map[duration:5s foo:bar num:123]`\n\n#### Single match\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/oriser/regroup\"\n\t\"time\"\n)\n\nvar re = regroup.MustCompile(`(?P\u003cduration\u003e.*?)\\s+(?P\u003cnum\u003e\\d+)\\s+(?P\u003cfoo\u003e.*)`)\n\ntype B struct {\n\tStr string `regroup:\"foo\"`\n}\n\ntype A struct {\n\tNumber        int           `regroup:\"num\"`\n\tDur           time.Duration `regroup:\"duration\"`\n\tAnotherStruct B\n}\n\nfunc main() {\n\ta := \u0026A{}\n\tif err := re.MatchToTarget(\"5s 123 bar\", a); err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Printf(\"%+v\\n\", a)\n}\n\n```\nWill output:\n`\u0026{Number:123 Dur:5s AnotherStruct:{Str:bar}}`\n\n#### Multiple matches\nYou can also get all matches parsed as given target struct. The return value in this\ncase will be an array of interfaces, you should cast it to the target type in order to access its fields.\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/oriser/regroup\"\n\t\"time\"\n)\n\nvar re = regroup.MustCompile(`\\s*(?P\u003cduration\u003e.*?)\\s+(?P\u003cnum\u003e\\d+)\\s+(?P\u003cfoo\u003e.*)`)\n\ntype B struct {\n\tStr string `regroup:\"foo\"`\n}\n\ntype A struct {\n\tNumber        int           `regroup:\"num\"`\n\tDur           time.Duration `regroup:\"duration\"`\n\tAnotherStruct B\n}\n\nfunc main() {\n\ta := \u0026A{}\n\ts := `5s 123 bar1\n\t\t  1m 456 bar2\n\t\t  10h 789 bar3`\n\trets, err := re.MatchAllToTarget(s, -1, a)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfor _, elem := range rets {\n\t\tfmt.Printf(\"%+v\\n\", elem.(*A))\n\t}\n}\n\n```\nWill output:\n```\n\u0026{Number:123 Dur:5s AnotherStruct:{Str:bar1}}\n\u0026{Number:456 Dur:1m0s AnotherStruct:{Str:bar2}}\n\u0026{Number:789 Dur:10h0m0s AnotherStruct:{Str:bar3}}\n```\n\n#### Required groups\nYou can specify that a specific group is required, means that it can't be empty.\n\nIf a required group is empty, an error (`*regroup.RequiredGroupIsEmpty`) will be returned .\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/oriser/regroup\"\n\t\"time\"\n)\n\nvar re = regroup.MustCompile(`(?P\u003cduration\u003e.*?)\\s+(?P\u003cnum\u003e\\d+)\\s+(?P\u003cfoo\u003e.*)`)\n\ntype B struct {\n\tStr string `regroup:\"foo,required\"`\n}\n\ntype A struct {\n\tNumber        int           `regroup:\"num\"`\n\tDur           time.Duration `regroup:\"duration\"`\n\tAnotherStruct B\n}\n\nfunc main() {\n\ta := \u0026A{}\n\tif err := re.MatchToTarget(\"5s 123 \", a); err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Printf(\"%+v\\n\", a)\n}\n```\nWill return an error: `required regroup \"foo\" is empty for field \"Str\"`\n\n### Existence Match Groups\nYou can check for the presence of an optional group using a `bool` with the `exists` tag.\n\n```go\npackage main\n\ntype Exist struct {\n\tIsAdmin bool `regroup:\"is_admin,exists\"`\n}\n\nfunc main() {\n\tr := MustCompile(`^(?P\u003cname\u003e\\w*)(?:,(?P\u003cis_admin\u003eadmin))?$`)\n\tparsed := \u0026Exist{}\n\tif err := r.MatchToTarget(\"bob_smith\", parsed); err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Printf(\"%t\\n\", parsed.IsAdmin)\n}\n```\n\nThis example would print `false`. However if the input were `bob_smith,admin` it would print `true`. When using the `exists` tag, make ure that you regular expression has an optional group and matches all the expected input patterns.\n\n## Supported struct field types\n- `time.Duration`\n- `bool`\n- `string`\n- `int`\n- `int8`\n- `int16`\n- `int32`\n- `int64`\n- `uint`\n- `uint8`\n- `uint16`\n- `uint32`\n- `uint64`\n- `float32`\n- `float64`\n\nPointers and nested structs are also supported, both on single match and multiple matches\n","funding_links":[],"categories":["Text Processing","Bot Building","文本处理","Template Engines","文本处理`解析和操作文本的代码库`","Utility"],"sub_categories":["Regular Expressions","正则表达式","查询语","HTTP Clients"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foriser%2Fregroup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foriser%2Fregroup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foriser%2Fregroup/lists"}