{"id":20515956,"url":"https://github.com/zyedidia/sregx","last_synced_at":"2025-04-14T00:33:52.977Z","repository":{"id":40539712,"uuid":"334545937","full_name":"zyedidia/sregx","owner":"zyedidia","description":"A tool and library for using structural regular expressions.","archived":false,"fork":false,"pushed_at":"2023-07-25T03:05:49.000Z","size":39,"stargazers_count":59,"open_issues_count":2,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-06-19T02:09:39.668Z","etag":null,"topics":["golang","regular-expressions","structural-regex","structural-regular-expressions"],"latest_commit_sha":null,"homepage":"","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/zyedidia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2021-01-31T01:06:25.000Z","updated_at":"2024-05-24T05:47:55.000Z","dependencies_parsed_at":"2024-06-19T02:07:20.218Z","dependency_job_id":null,"html_url":"https://github.com/zyedidia/sregx","commit_stats":null,"previous_names":["zyedidia/sre"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zyedidia%2Fsregx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zyedidia%2Fsregx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zyedidia%2Fsregx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zyedidia%2Fsregx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zyedidia","download_url":"https://codeload.github.com/zyedidia/sregx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224846906,"owners_count":17379627,"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-expressions","structural-regex","structural-regular-expressions"],"created_at":"2024-11-15T21:25:44.429Z","updated_at":"2024-11-15T21:25:45.170Z","avatar_url":"https://github.com/zyedidia.png","language":"Go","funding_links":[],"categories":["Influenced by Plan9"],"sub_categories":["Utilities"],"readme":"# Structural Regular Expressions\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/zyedidia/sregx.svg)](https://pkg.go.dev/github.com/zyedidia/sregx)\n[![Go Report Card](https://goreportcard.com/badge/github.com/zyedidia/sregx)](https://goreportcard.com/report/github.com/zyedidia/sregx)\n[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/zyedidia/sregx/blob/master/LICENSE)\n\nsregx is a package and tool for using structural regular expressions as described\nby Rob Pike ([link](http://doc.cat-v.org/bell_labs/structural_regexps/)). sregx\nprovides a very simple Go package for creating structural regular expression\ncommands as well as a library for parsing and compiling sregx commands from the\ntext format used in Pike's description. A CLI tool for using structural regular\nexpressions is also provided in `./cmd/sregx`, allowing you to perform advanced\ntext manipulation from the command-line.\n\nIn a structural regular expression, regular expressions are composed using\ncommands to perform tasks like advanced search and replace. A command has\nan input string and produces an output string. The following commands are\nsupported:\n\n* `p`: prints the input string, and then returns the input string.\n* `d`: returns the empty string.\n* `c/\u003cs\u003e/`: returns the string `\u003cs\u003e`.\n* `s/\u003cp\u003e/\u003cs\u003e/`: returns a string where substrings matching the regular\n  expression `\u003cp\u003e` have been replaced with `\u003cs\u003e`.\n* `g/\u003cp\u003e/\u003ccmd\u003e`: if `\u003cp\u003e` matches the input, returns the result of `\u003ccmd\u003e`\n  evaluated on the input. Otherwise returns the input with no modification.\n* `v/\u003cp\u003e/\u003ccmd\u003e`: if `\u003cp\u003e` does not match the input, returns the result of\n  `\u003ccmd\u003e` evaluated on the input. Otherwise returns the input with no\n  modification.\n* `x/\u003cp\u003e/\u003ccmd\u003e`: returns a string where all substrings matching the regular\n  expression `\u003cp\u003e` have been replaced with the return value of `\u003ccmd\u003e` applied\n  to the particular substring.\n* `y/\u003cp\u003e/\u003ccmd\u003e`: returns a string where each part of the string that is not\n  matched by `\u003cp\u003e` is replaced by applying `\u003ccmd\u003e` to the particular\n  unmatched string.\n* `n[N:M]\u003ccmd\u003e`: returns the application of `\u003ccmd\u003e` to the input sliced from\n  `[N:M)`. Accepts negative numbers to refer to offsets from the end of the\n  input. Offsets are zero-indexed.\n* `l[N:M]\u003ccmd\u003e`: returns the application of `\u003ccmd\u003e` to the input sliced from\n  line `N` to line `M` (exclusive).  Assumes newlines are represented with the\n  `\\n` character. Accepts negative numbers to refer to offsets from the last\n  line of the input. Lines are zero-indexed.\n* `u/\u003csh\u003e/`: executes the shell command `\u003csh\u003e` with the input as stdin and\n  returns the resulting stdout of the command. Shell commands use a simple\n  syntax where single or double quotes can be used to group arguments, and\n  environment variables are accessible with `$`. This command is only directly\n  available as part of the sregx CLI tool.\n\nThe commands `n[...]`, `l[...]`, and `u` are additions to the original\ndescription of structural regular expressions.\n\nThe sregx tool also provides another augmentation to the original sregx description\nfrom Pike: command pipelines. A command may be given as `\u003ccmd\u003e | \u003ccmd\u003e | ...`\nwhere the input of each command is the output of the previous one.\n\n### Examples\n\nMost of these examples are from Pike's description, so you can look there for\nmore detailed explanation. Since `p` is the only command that prints,\ntechnically you must append `| p` to commands that search and replace, because\notherwise nothing will be printed. However, since you will probably forget to\ndo this, the sregx tool will print the result of the final command before\nterminating if there were no uses of `p` anywhere within the command. Thus when\nusing the CLI tool you can omit the `| p` in the following commands and still\nsee the result.\n\nPrint all lines that contain \"string\":\n\n```\nx/.*\\n/ g/string/p\n```\n\nDelete all occurrences of \"string\" and print the result:\n\n```\nx/string/d | p\n```\n\nReplace all occurrences of \"foo\" with \"bar\" in the range of lines 5-10\n(zero-indexed):\n\n```\nl[5:10]s/foo/bar/ | p\n```\n\nPrint all lines containing \"rob\" but not \"robot\":\n\n```\nx/.*\\n/ g/rob/ v/robot/p\n```\n\nCapitalize all occurrences of the word \"i\":\n\n```\nx/[A-Za-z]+/ g/i/ v/../ c/I/ | p\n```\n\nor (more simply)\n\n```\nx/[A-Za-z]+/ g/^i$/ c/I/ | p\n```\n\nPrint the last line of every paragraph that begins with \"foo\", where a\nparagraph is defined as text with no empty lines:\n\n```\nx/(.+\\n)+/ g/^foo/ l[-2:-1]p\n```\n\nChange all occurrences of the complete word \"foo\" to \"bar\" except those\noccurring in double or single quoted strings:\n\n```\ny/\".*\"/ y/'.*'/ x/[a-zA-Z]+/ g/^foo$/ c/bar/ | p\n```\n\nReplace the complete word \"TODAY\" with the current date:\n\n```\nx/[A-Z]+/ g/^TODAY$/ u/date/ | p\n```\n\nCapitalize all words:\n\n```\nx/[a-zA-Z]+/ x/^./ u/tr a-z A-Z/ | p\n```\n\nNote: it is highly recommended when using the CLI tool that you enclose\nexpressions in single or double quotes to prevent your shell from interpreting\nspecial characters.\n\n## Installation\n\nThere are three ways to install `sregx`.\n\n1. Download the prebuilt binary from the releases page (comes with man file).\n\n2. Install from source:\n\n```\ngit clone https://github.com/zyedidia/sregx\ncd sregx\nmake build # or make install to install to $GOBIN\n```\n\n3. Install with `go get` (version info will be missing):\n\n```\ngo get github.com/zyedidia/sregx/cmd/sregx\n```\n\n### Usage\n\nTo use the CLI tool, first pass the expression and then the input file. If no\nfile is given, stdin will be used. Here is an example to capitalize all\noccurrences of the word 'i' in `file.txt`:\n\n```\nsregx 'x/[A-Za-z]+/ g/^i$/ c/I/' file.txt\n```\n\nThe tool tries to provide high quality error messages when you make a mistake\nin the expression syntax.\n\n## Base library\n\nThe base library is very simple and small (roughly 100 lines of code). In fact,\nit is surprisingly simple and elegant for something that can provide such\npowerful text manipulation, and I recommend reading the code if you are\ninterested. Each type of command may be manually created directly in tree form.\nSee the Go documentation for details.\n\n## Syntax library\n\nThe syntax library supports parsing and compiling a string into a structural\nregular expression command. The syntax follows certain rules, such as using \"/\"\nas a delimiter. The backslash (`\\`) may be used to escape `/` or `\\`, or to\ncreate special characters such as `\\n`, `\\r`, or `\\t`. The syntax also supports\nspecifying arbitrary bytes using octal, for example `\\14`. Regular expressions\nuse the Go syntax described [here](https://golang.org/pkg/regexp/syntax/).\n\n# Future Work\n\nHere are some ideas for some features that could be implemented in the future.\n\n* Internal manipulation language. Currently the `u` command runs shell\n  commands. This is very flexible but can be costly because a new process is\n  run to perform each transformation. For better performance we could provide a\n  small language that has some string manipulation functions like `toupper`. A\n  good candidate for this language would be Lua. This would also improve\n  Windows support since most Windows environments lack utilities like `tr`.\n* Different regex engine. The Go regex engine is pretty good, but isn't\n  especially performant. We could switch to Oniguruma (see the `oniguruma`\n  branch), although this would mean using cgo.\n* Structural PEGs. Use PEGs instead of regular expressions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzyedidia%2Fsregx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzyedidia%2Fsregx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzyedidia%2Fsregx/lists"}