{"id":13502933,"url":"https://github.com/Canop/lazy-regex","last_synced_at":"2025-03-29T12:33:14.557Z","repository":{"id":41866192,"uuid":"213437345","full_name":"Canop/lazy-regex","owner":"Canop","description":"lazy static regular expressions checked at compile time","archived":false,"fork":false,"pushed_at":"2024-12-27T06:53:47.000Z","size":95,"stargazers_count":205,"open_issues_count":5,"forks_count":14,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-23T05:26:43.486Z","etag":null,"topics":["compilation","regular-expression","rust","static"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/Canop.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":["Canop"]}},"created_at":"2019-10-07T16:50:30.000Z","updated_at":"2025-03-22T22:15:37.000Z","dependencies_parsed_at":"2024-04-01T14:57:19.070Z","dependency_job_id":"3a7eec52-6a0d-4f54-b05e-83d9594fce01","html_url":"https://github.com/Canop/lazy-regex","commit_stats":{"total_commits":48,"total_committers":8,"mean_commits":6.0,"dds":0.1875,"last_synced_commit":"38424d37f03546df0823a6eaf8521f6c1fd15b58"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Canop%2Flazy-regex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Canop%2Flazy-regex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Canop%2Flazy-regex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Canop%2Flazy-regex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Canop","download_url":"https://codeload.github.com/Canop/lazy-regex/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246187190,"owners_count":20737459,"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":["compilation","regular-expression","rust","static"],"created_at":"2024-07-31T22:02:30.863Z","updated_at":"2025-03-29T12:33:14.313Z","avatar_url":"https://github.com/Canop.png","language":"Rust","funding_links":["https://github.com/sponsors/Canop"],"categories":["Rust"],"sub_categories":[],"readme":"[![MIT][s2]][l2] [![Latest Version][s1]][l1] [![docs][s3]][l3] [![Chat on Miaou][s4]][l4]\n\n[s1]: https://img.shields.io/crates/v/lazy-regex.svg\n[l1]: https://crates.io/crates/lazy-regex\n\n[s2]: https://img.shields.io/badge/license-MIT-blue.svg\n[l2]: LICENSE\n\n[s3]: https://docs.rs/lazy-regex/badge.svg\n[l3]: https://docs.rs/lazy-regex/\n\n[s4]: https://miaou.dystroy.org/static/shields/room.svg\n[l4]: https://miaou.dystroy.org/3\n\n\n# lazy-regex\n\nWith lazy-regex macros, regular expressions\n\n* are checked at compile time, with clear error messages\n* are wrapped in `once_cell` lazy static initializers so that they're compiled only once\n* can hold flags as suffix: `let case_insensitive_regex = regex!(\"ab*\"i);`\n* are defined in a less verbose way\n\nThe `regex!` macro returns references to normal instances of `regex::Regex` or `regex::bytes::Regex` so all the usual features are available.\n\nOther macros are specialized for testing a match, replacing with concise closures, or capturing groups as substrings in some common situations:\n\n* `regex_is_match!`\n* `regex_find!`\n* `regex_captures!`\n* `regex_replace!`\n* `regex_replace_all!`\n* `regex_switch!`\n\nThey support the `B` flag for the `regex::bytes::Regex` variant.\n\nAll macros exist with a `bytes_` prefix for building `bytes::Regex`, so you also have `bytes_regex!`, `bytes_regex_is_match!`, `bytes_regex_find!`, `bytes_regex_captures!`, `bytes_regex_replace!`, `bytes_regex_replace_all!`, and `bytes_regex_switch!`.\n\nSome structs of the regex crate are reexported to ease dependency managment.\nThe regex crate itself is also reexported, to avoid the need to synchronize the versions/flavor (see [Features](#features_and_reexport) below)\n\n# Build Regexes\n\n```rust\nuse lazy_regex::regex;\n\n// build a simple regex\nlet r = regex!(\"sa+$\");\nassert_eq!(r.is_match(\"Saa\"), false);\n\n// build a regex with flag(s)\nlet r = regex!(\"sa+$\"i);\nassert_eq!(r.is_match(\"Saa\"), true);\n\n// you can use a raw literal\nlet r = regex!(r#\"^\"+$\"#);\nassert_eq!(r.is_match(\"\\\"\\\"\"), true);\n\n// or a raw literal with flag(s)\nlet r = regex!(r#\"^\\s*(\"[a-t]*\"\\s*)+$\"#i);\nassert_eq!(r.is_match(r#\" \"Aristote\" \"Platon\" \"#), true);\n\n// build a regex that operates on \u0026[u8]\nlet r = regex!(\"(byte)?string$\"B);\nassert_eq!(r.is_match(b\"bytestring\"), true);\n\n// there's no problem using the multiline definition syntax\nlet r = regex!(r#\"(?x)\n    (?P\u003cname\u003e\\w+)\n    -\n    (?P\u003cversion\u003e[0-9.]+)\n\"#);\nassert_eq!(r.find(\"This is lazy_regex-2.2!\").unwrap().as_str(), \"lazy_regex-2.2\");\n// (look at the regex_captures! macro to easily extract the groups)\n\n```\n```compile_fail\n// this line doesn't compile because the regex is invalid:\nlet r = regex!(\"(unclosed\");\n\n```\nSupported regex flags: `i`, `m`, `s`, `x`, `U`.\n\nSee [regex::RegexBuilder](https://docs.rs/regex/latest/regex/struct.RegexBuilder.html).\n\n# Test a match\n\n```rust\nuse lazy_regex::regex_is_match;\n\nlet b = regex_is_match!(\"[ab]+\", \"car\");\nassert_eq!(b, true);\n```\n\n\n# Extract a value\n\n```rust\nuse lazy_regex::regex_find;\n\nlet f_word = regex_find!(r#\"\\bf\\w+\\b\"#, \"The fox jumps.\");\nassert_eq!(f_word, Some(\"fox\"));\nlet f_word = regex_find!(r#\"\\bf\\w+\\b\"#B, b\"The forest is silent.\");\nassert_eq!(f_word, Some(b\"forest\" as \u0026[u8]));\n```\n\n# Capture\n\n```rust\nuse lazy_regex::regex_captures;\n\nlet (_, letter) = regex_captures!(\"([a-z])[0-9]+\"i, \"form A42\").unwrap();\nassert_eq!(letter, \"A\");\n\nlet (whole, name, version) = regex_captures!(\n    r#\"(\\w+)-([0-9.]+)\"#, // a literal regex\n    \"This is lazy_regex-2.0!\", // any expression\n).unwrap();\nassert_eq!(whole, \"lazy_regex-2.0\");\nassert_eq!(name, \"lazy_regex\");\nassert_eq!(version, \"2.0\");\n```\n\nThere's no limit to the size of the tuple.\nIt's checked at compile time to ensure you have the right number of capturing groups.\n\nYou receive `\"\"` for optional groups with no value.\n\n# Replace with captured groups\n\nThe `regex_replace!` and `regex_replace_all!` macros bring once compilation and compilation time checks to the `replace` and `replace_all` functions.\n\n## Replace with a closure\n\n```rust\nuse lazy_regex::regex_replace_all;\n\nlet text = \"Foo8 fuu3\";\nlet text = regex_replace_all!(\n    r#\"\\bf(\\w+)(\\d)\"#i,\n    text,\n    |_, name, digit| format!(\"F\u003c{}\u003e{}\", name, digit),\n);\nassert_eq!(text, \"F\u003coo\u003e8 F\u003cuu\u003e3\");\n```\nThe number of arguments given to the closure is checked at compilation time to match the number of groups in the regular expression.\n\nIf it doesn't match you get, at compilation time, a clear error message.\n\n## Replace with another kind of Replacer\n\n```rust\nuse lazy_regex::regex_replace_all;\nlet text = \"UwU\";\nlet output = regex_replace_all!(\"U\", text, \"O\");\nassert_eq!(\u0026output, \"OwO\");\n```\n\n# Switch over regexes\n\nExecute the expression bound to the first matching regex, with named captured groups declared as varibles:\n\n```rust\nuse lazy_regex::regex_switch;\npub enum ScrollCommand {\n    Top,\n    Bottom,\n    Lines(i32),\n    Pages(i32),\n}\nimpl std::str::FromStr for ScrollCommand {\n    type Err = ();\n    fn from_str(s: \u0026str) -\u003e Result\u003cSelf, ()\u003e {\n        regex_switch!(s,\n            \"^scroll-to-top$\" =\u003e Self::Top,\n            \"^scroll-to-bottom$\" =\u003e Self::Bottom,\n            r#\"^scroll-lines?\\((?\u003cn\u003e[+-]?\\d{1,4})\\)$\"# =\u003e Self::Lines(n.parse().unwrap()),\n            r#\"^scroll-pages?\\((?\u003cn\u003e[+-]?\\d{1,4})\\)$\"# =\u003e Self::Pages(n.parse().unwrap()),\n        ).ok_or(())\n    }\n}\n```\n\n# Shared lazy static\n\nWhen a regular expression is used in several functions, you sometimes don't want\nto repeat it but have a shared static instance.\n\nThe `regex!` macro, while being backed by a lazy static regex, returns a reference.\n\nIf you want to have a shared lazy static regex, use the `lazy_regex!` macro:\n\n```rust\nuse lazy_regex::*;\n\npub static GLOBAL_REX: Lazy\u003cRegex\u003e = lazy_regex!(\"^ab+$\"i);\n```\n\nLike for the other macros, the regex is static, checked at compile time, and lazily built at first use.\n\n# Features and reexport\n\nWith default features, `lazy-regex` use the `regex` crate with its default features, tailored for performances and complete Unicode support.\n\nYou may enable a different set of regex features by directly enabling them when importing `lazy-regex`.\n\nIt's also possible to use the [regex-lite](https://docs.rs/regex-lite/) crate instead of the [regex](https://docs.rs/regex/) crate by declaring the ``lite`` feature:\n\n```TOML\nlazy-regex = { version = \"3.0\", default-features = false, features = [\"lite\"] }\n```\n\nThe `lite` flavor comes with slightly lower performances and a reduced Unicode support (see crate documentation) but also a much smaller binary size.\n\nIf you need to refer to the regex crate in your code, prefer to use the reexport (i.e. `use lazy_regex::regex;`) so that you don't have a version or flavor conflict. When the `lite` feature is enabled, `lazy_regex::regex` refers to `regex_lite` so you don't have to change your code when switching regex engine.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCanop%2Flazy-regex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCanop%2Flazy-regex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCanop%2Flazy-regex/lists"}