{"id":13827591,"url":"https://github.com/alexflint/go-restructure","last_synced_at":"2025-05-15T09:07:38.007Z","repository":{"id":48911014,"uuid":"50303427","full_name":"alexflint/go-restructure","owner":"alexflint","description":"Match regular expressions into struct fields","archived":false,"fork":false,"pushed_at":"2024-12-02T22:08:13.000Z","size":86,"stargazers_count":591,"open_issues_count":6,"forks_count":16,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-05-12T05:39:45.070Z","etag":null,"topics":["golang","regular-expression"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexflint.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2016-01-24T19:17:45.000Z","updated_at":"2025-04-13T15:37:35.000Z","dependencies_parsed_at":"2025-02-21T21:11:04.178Z","dependency_job_id":null,"html_url":"https://github.com/alexflint/go-restructure","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexflint%2Fgo-restructure","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexflint%2Fgo-restructure/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexflint%2Fgo-restructure/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexflint%2Fgo-restructure/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexflint","download_url":"https://codeload.github.com/alexflint/go-restructure/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254310515,"owners_count":22049469,"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":["golang","regular-expression"],"created_at":"2024-08-04T09:02:02.668Z","updated_at":"2025-05-15T09:07:32.999Z","avatar_url":"https://github.com/alexflint.png","language":"Go","readme":"\u003ch4 align=\"center\"\u003eStruct-based regular expressions for Go\u003c/h4\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://pkg.go.dev/github.com/alexflint/go-restructure\"\u003e\u003cimg src=\"https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\u0026style=flat-square\" alt=\"Documentation\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/alexflint/go-restructure/actions\"\u003e\u003cimg src=\"https://github.com/alexflint/go-restructure/workflows/Go/badge.svg\" alt=\"Build Status\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://codecov.io/gh/alexflint/go-restructure\"\u003e\u003cimg src=\"https://codecov.io/gh/alexflint/go-restructure/branch/master/graph/badge.svg\" alt=\"Coverage Status\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://goreportcard.com/report/github.com/alexflint/go-restructure\"\u003e\u003cimg src=\"https://goreportcard.com/badge/github.com/alexflint/go-restructure\" alt=\"Go Report Card\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\u003cbr\u003e\n\n## Match regular expressions into struct fields\n\n```shell\ngo get github.com/alexflint/go-restructure\n```\n\nThis package allows you to express regular expressions by defining a struct, and then capture matched sub-expressions into struct fields. Here is a very simple email address parser:\n\n```go\nimport \"github.com/alexflint/go-restructure\"\n\ntype EmailAddress struct {\n\t_    struct{} `^`\n\tUser string   `\\w+`\n\t_    struct{} `@`\n\tHost string   `[^@]+`\n\t_    struct{} `$`\n}\n\nfunc main() {\n\tvar addr EmailAddress\n\trestructure.Find(\u0026addr, \"joe@example.com\")\n\tfmt.Println(addr.User) // prints \"joe\"\n\tfmt.Println(addr.Host) // prints \"example.com\"\n}\n```\n(Note that the above is far too simplistic to be used as a serious email address validator.)\n\nThe regular expression that was executed was the concatenation of the struct tags:\n\n```\n^(\\w+)@([^@]+)$\n```\n\nThe first submatch was inserted into the `User` field and the second into the `Host` field.\n\nYou may also use the `regexp:` tag key, but keep in mind that you must escape quotes and backslashes:\n\n```go\ntype EmailAddress struct {\n\t_    string `regexp:\"^\"`\n\tUser string `regexp:\"\\\\w+\"`\n\t_    string `regexp:\"@\"`\n\tHost string `regexp:\"[^@]+\"`\n\t_    string `regexp:\"$\"`\n}\n```\n\n### Nested Structs\n\nHere is a slightly more sophisticated email address parser that uses nested structs:\n\n```go\ntype Hostname struct {\n\tDomain string   `\\w+`\n\t_      struct{} `\\.`\n\tTLD    string   `\\w+`\n}\n\ntype EmailAddress struct {\n\t_    struct{} `^`\n\tUser string   `[a-zA-Z0-9._%+-]+`\n\t_    struct{} `@`\n\tHost *Hostname\n\t_    struct{} `$`\n}\n\nfunc main() {\n\tvar addr EmailAddress\n\tsuccess, _ := restructure.Find(\u0026addr, \"joe@example.com\")\n\tif success {\n\t\tfmt.Println(addr.User)        // prints \"joe\"\n\t\tfmt.Println(addr.Host.Domain) // prints \"example\"\n\t\tfmt.Println(addr.Host.TLD)    // prints \"com\"\n\t}\n}\n```\n\nCompare this to using the standard library `regexp.FindStringSubmatchIndex` directly:\n\n```go\nfunc main() {\n\tcontent := \"joe@example.com\"\n\texpr := regexp.MustCompile(`^([a-zA-Z0-9._%+-]+)@((\\w+)\\.(\\w+))$`)\n\tindices := expr.FindStringSubmatchIndex(content)\n\tif len(indices) \u003e 0 {\n\t\tuserBegin, userEnd := indices[2], indices[3]\n\t\tvar user string\n\t\tif userBegin != -1 \u0026\u0026 userEnd != -1 {\n\t\t\tuser = content[userBegin:userEnd]\n\t\t}\n\n\t\tdomainBegin, domainEnd := indices[6], indices[7]\n\t\tvar domain string\n\t\tif domainBegin != -1 \u0026\u0026 domainEnd != -1 {\n\t\t\tdomain = content[domainBegin:domainEnd]\n\t\t}\n\n\t\ttldBegin, tldEnd := indices[8], indices[9]\n\t\tvar tld string\n\t\tif tldBegin != -1 \u0026\u0026 tldEnd != -1 {\n\t\t\ttld = content[tldBegin:tldEnd]\n\t\t}\n\n\t\tfmt.Println(user)   // prints \"joe\"\n\t\tfmt.Println(domain) // prints \"example\"\n\t\tfmt.Println(tld)    // prints \"com\"\n\t}\n}\n```\n\n### Ints\n\nIt is also possible to set struct fields as `int` to get the string automatically converted.\n\n```go\n// Matches \"12 wombats\", \"1 wombat\" and store the number as int\ntype Wisdom struct {\n\tNumber   int       `^\\d+`\n\t_   \t string    `\\s+`\n\tAnimal   string    `\\w+`\n}\n```\n\n### Optional fields\n\nWhen nesting one struct within another, you can make the nested struct optional by marking it with `?`. The following example parses floating point numbers with optional sign and exponent:\n\n```go\n// Matches \"123\", \"1.23\", \"1.23e-4\", \"-12.3E+5\", \".123\"\ntype Float struct {\n\tSign     *Sign     `?`      // sign is optional\n\tWhole    string    `[0-9]*`\n\tPeriod   struct{}  `\\.?`\n\tFrac     string    `[0-9]+`\n\tExponent *Exponent `?`      // exponent is optional\n}\n\n// Matches \"e+4\", \"E6\", \"e-03\"\ntype Exponent struct {\n\t_    struct{} `[eE]`\n\tSign *Sign    `?`         // sign is optional\n\tNum  string   `[0-9]+`\n}\n\n// Matches \"+\" or \"-\"\ntype Sign struct {\n\tCh string `[+-]`\n}\n```\n\nWhen an optional sub-struct is not matched, it will be set to nil:\n\n```javascript\n\"1.23\" -\u003e {\n  \"Sign\": nil,\n  \"Whole\": \"1\",\n  \"Frac\": \"23\",\n  \"Exponent\": nil\n}\n\n\"1.23e+45\" -\u003e {\n  \"Sign\": nil,\n  \"Whole\": \"1\",\n  \"Frac\": \"23\",\n  \"Exponent\": {\n    \"Sign\": {\n      \"Ch\": \"+\"\n    },\n    \"Num\": \"45\"\n  }\n}\n```\n\n### Finding multiple matches\n\nThe following example uses `Regexp.FindAll` to extract all floating point numbers from\na string, using the same `Float` struct as in the example above.\n\n```go\nsrc := \"There are 10.4 cats for every 100 dogs in the United States.\"\nfloatRegexp := restructure.MustCompile(Float{}, restructure.Options{})\nvar floats []Float\nfloatRegexp.FindAll(\u0026floats, src, -1)\n```\n\nTo limit the number of matches set the third parameter to a positive number.\n\n### Getting begin and end positions for submatches\n\nTo get the begin and end position of submatches, use the `restructure.Submatch` struct in place of `string`:\n\nHere is an example of matching python imports such as `import foo as bar`:\n\n```go\ntype Import struct {\n\t_       struct{}             `^import\\s+`\n\tPackage restructure.Submatch `\\w+`\n\t_       struct{}             `\\s+as\\s+`\n\tAlias   restructure.Submatch `\\w+`\n}\n\nvar importRegexp = restructure.MustCompile(Import{}, restructure.Options{})\n\nfunc main() {\n\tvar imp Import\n\timportRegexp.Find(\u0026imp, \"import foo as bar\")\n\tfmt.Printf(\"IMPORT %s (bytes %d...%d)\\n\", imp.Package.String(), imp.Package.Begin, imp.Package.End)\n\tfmt.Printf(\"    AS %s (bytes %d...%d)\\n\", imp.Alias.String(), imp.Alias.Begin, imp.Alias.End)\n}\n```\nOutput:\n```\nIMPORT foo (bytes 7...10)\n    AS bar (bytes 14...17)\n```\n\n### Regular expressions inside JSON\n\nTo run a regular expression as part of a json unmarshal, just implement the `JSONUnmarshaler` interface. Here is an example that parses the following JSON string containing a quaternion:\n\n```javascript\n{\n\t\"Var\": \"foo\",\n\t\"Val\": \"1+2i+3j+4k\"\n}\n```\n\nFirst we define the expressions for matching quaternions in the form `1+2i+3j+4k`:\n\n```go\n// Matches \"1\", \"-12\", \"+12\"\ntype RealPart struct {\n\tSign string `regexp:\"[+-]?\"`\n\tReal string `regexp:\"[0-9]+\"`\n}\n\n// Matches \"+123\", \"-1\"\ntype SignedInt struct {\n\tSign string `regexp:\"[+-]\"`\n\tReal string `regexp:\"[0-9]+\"`\n}\n\n// Matches \"+12i\", \"-123i\"\ntype IPart struct {\n\tMagnitude SignedInt\n\t_         struct{} `regexp:\"i\"`\n}\n\n// Matches \"+12j\", \"-123j\"\ntype JPart struct {\n\tMagnitude SignedInt\n\t_         struct{} `regexp:\"j\"`\n}\n\n// Matches \"+12k\", \"-123k\"\ntype KPart struct {\n\tMagnitude SignedInt\n\t_         struct{} `regexp:\"k\"`\n}\n\n// matches \"1+2i+3j+4k\", \"-1+2k\", \"-1\", etc\ntype Quaternion struct {\n\tReal *RealPart\n\tI    *IPart `regexp:\"?\"`\n\tJ    *JPart `regexp:\"?\"`\n\tK    *KPart `regexp:\"?\"`\n}\n\n// matches the quoted strings `\"-1+2i\"`, `\"3-4i\"`, `\"12+34i\"`, etc\ntype QuotedQuaternion struct {\n\t_          struct{} `regexp:\"^\"`\n\t_          struct{} `regexp:\"\\\"\"`\n\tQuaternion *Quaternion\n\t_          struct{} `regexp:\"\\\"\"`\n\t_          struct{} `regexp:\"$\"`\n}\n```\n\nNext we implement `UnmarshalJSON` for the `QuotedQuaternion` type:\n```go\nvar quaternionRegexp = restructure.MustCompile(QuotedQuaternion{}, restructure.Options{})\n\nfunc (c *QuotedQuaternion) UnmarshalJSON(b []byte) error {\n\tif !quaternionRegexp.Find(c, string(b)) {\n\t\treturn fmt.Errorf(\"%s is not a quaternion\", string(b))\n\t}\n\treturn nil\n}\n\n```\n\nNow we can define a struct and unmarshal JSON into it:\n```go\ntype Var struct {\n\tName  string\n\tValue *QuotedQuaternion\n}\n\nfunc main() {\n\tsrc := `{\"name\": \"foo\", \"value\": \"1+2i+3j+4k\"}`\n\tvar v Var\n\tjson.Unmarshal([]byte(src), \u0026v)\n}\n```\nThe result is:\n```javascript\n{\n  \"Name\": \"foo\",\n  \"Value\": {\n    \"Quaternion\": {\n      \"Real\": {\n        \"Sign\": \"\",\n        \"Real\": \"1\"\n      },\n      \"I\": {\n        \"Magnitude\": {\n          \"Sign\": \"+\",\n          \"Real\": \"2\"\n        }\n      },\n      \"J\": {\n        \"Magnitude\": {\n          \"Sign\": \"+\",\n          \"Real\": \"3\"\n        }\n      },\n      \"K\": {\n        \"Magnitude\": {\n          \"Sign\": \"+\",\n          \"Real\": \"4\"\n        }\n      }\n    }\n  }\n}\n```\n\n### Index of examples\n\n- [Parse an email address](samples/simple-email/simple-email.go)\n- [Parse an email address using nested structs](samples/email-address/email-address.go)\n- [Parse a floating point number](samples/floating-point/floating-point.go)\n- [Find all floats in a string](samples/find-all-floats/find-all-floats.go)\n- [Parse a dotted name](samples/name-dot-name/name-dot-name.go)\n- [Parse a python import statement](samples/python-import/python-import.go)\n- [Regular expression inside a JSON struct](samples/quaternion-in-json/quaternion-in-json.go)\n\n### Benchmarks\n\nSee [benchmarks document](BENCHMARKS.md)\n","funding_links":[],"categories":["Go","Libraries","Library"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexflint%2Fgo-restructure","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexflint%2Fgo-restructure","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexflint%2Fgo-restructure/lists"}