Snapshot của Canop/lazy-regex: 230★ · Rust · Backend. lazy static regular expressions checked at compile time
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
are checked at compile time, with clear error messages
are wrapped in once_cell lazy static initializers so that they're compiled only once
can hold flags as suffix: let case_insensitive_regex = regex!("ab*"i);
are defined in a less verbose way
The regex! macro returns references to normal instances of regex::Regex or regex::bytes::Regex so all the usual features are available.
But most often, you won't even use the regex! macro but the other macros which are specialized for testing a match, replacing, or capturing groups in some common situations:
Test a match with regex_is_match!
Extract a value with regex_find!
Capture with regex_captures!
Iter on captures with regex_captures_iter!
Replace with captured groups with regex_replace! and regex_replace_all!
Remove part(s) of a string with regex_remove! and regex_remove_all!
Switch over patterns with regex_switch!
They support the B flag for the regex::bytes::Regex variant.
All 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!.
Some structs of the regex crate are reexported to ease dependency managment.
Build Regexes
Build a simple regex:
let r = regex!("sa+$");
assert_eq!(r.is_match("Saa"), false);
Build a regex with flag(s):
let r = regex!("sa+$"i);
assert_eq!(r.is_match("Saa"), true);
You can use a raw literal:
let r = regex!(r#"^"+$"#);
assert_eq!(r.is_match("\"\""), true);
Or a raw literal with flag(s):
let r = regex!(r#"^\s*("[a-t]*"\s*)+$"#i);
assert_eq!(r.is_match(r#" "Aristote" "Platon" "#), true);
Build a regex that operates on &[u8]:
let r = regex!("(byte)?string$"B);
assert_eq!(r.is_match(b"bytestring"), true);
There's no problem using the multiline definition syntax:
let r = regex!(r"(?x)
(?P<name>\w+)
-
(?P<version>[0-9.]+)
");
assert_eq!(r.find("This is lazy_regex-2.2!").unwrap().as_str(), "lazy_regex-2.2");
(look at the regex_captures! macro to easily extract the groups)
This line doesn't compile because the regex is invalid:
let r = regex!("(unclosed");
Supported regex flags: i, m, s, x, U, and you may also use B to build a bytes regex.
The following regexes are equivalent:
bytes_regex!("^ab+$"i)
bytes_regex!("(?i)^ab+$")
regex!("^ab+$"iB)
regex!("(?i)^ab+$"B)
They're all case insensitive instances of regex::bytes::Regex.
Test a match
let b = regex_is_match!("[ab]+", "car");
assert_eq!(b, true);
let b = bytes_regex_is_match!("[ab]+", b"car");
assert_eq!(b, true);
See regex_is_match!
Extract a value
let f_word = regex_find!(r"\bf\w+\b", "The fox jumps.");
assert_eq!(f_word, Some("fox"));
let f_word = regex_find!(r"\bf\w+\b"B, b"The forest is silent.");
assert_eq!(f_word, Some(b"forest" as &[u8]));
See regex_find!
Capture
let (_, letter) = regex_captures!("([a-z])[0-9]+"i, "form A42").unwrap();
assert_eq!(letter, "A");
let (whole, name, version) = regex_captures!(
r"(\w+)-([0-9.]+)", // a literal regex
"This is lazy_regex-2.0!", // any expression
).unwrap();
assert_eq!(whole, "lazy_regex-2.0");
assert_eq!(name, "lazy_regex");
assert_eq!(version, "2.0");
There's no limit to the size of the tuple.
It's checked at compile time to ensure you have the right number of capturing groups.
You receive "" for optional groups with no value.
See regex_captures!
Iter on captures
let hay = "'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931).";
let mut movies = vec![];
let iter = regex_captures_iter!(r"'([^']+)'\s+\(([0-9]{4})\)", hay);
for (_, [title, year]) in iter.map(|c| c.extract()) {
movies.push((title, year.parse::<i64>().unwrap()));
}
assert_eq!(movies, vec![
("Citizen Kane", 1941),
("The Wizard of Oz", 1939),
("M", 1931),
]);
See regex_captures_iter!
Replace with captured groups
The regex_replace! and regex_replace_all! macros bring once compilation and compilation time checks to the replace and replace_all functions.
Replace with a closure
let text = "Foo8 fuu3";
let text = regex_replace_all!(
r"\bf(\w+)(\d)"i,
text,
|_, name, digit| format!("F<{}>{}", name, digit),
);
assert_eq!(text, "F<oo>8 F<uu>3");
The number of arguments given to the closure is checked at compilation time to match the number of groups in the regular expression.
If it doesn't match you get a clear error message at compilation time.
Replace with another kind of Replacer
let text = "UwU";
let output = regex_replace_all!("U", text, "O");
assert_eq!(&output, "OwO");
Remove part of a string
When you want to remove part of a string matching a regex, don't use regex_replace! with an empty string, but rather the regex_remove! macro which is more efficient and returns a borrowed slice when possible.
Contrary to replace, lazy-regex removing macros don't allocate a new string if the match is at an end of the input, which makes it especially useful for trimming suffixes or prefixes.
let text = "lazy-regex-3.5.0";
let name = regex_remove!(
r"-[0-9]+(\.[0-9]+)*$",
text,
);
assert_eq!(name, "lazy-regex");
assert!(matches!(name, std::borrow::Cow::Borrowed(_)));
You can also remove all matches of a regex with regex_remove_all!.
assert_eq!(
regex_remove_all!(r"\s+", " ab c d e "),
"abcde"
);
Whenever possible, no new string is allocated and a borrowed slice is returned, even when several matches are removed
(if they're all either at the start or end of the text).
let input = "154681string63731";
let output = regex_remove_all!(r"\d", input);
assert_eq!(output, "string");
assert!(matches!(output, std::borrow::Cow::Borrowed("string")));
Switch over patterns
Execute the expression bound to the first matching regex, with named captured groups declared as variables:
Like for the other macros, the regex is static, checked at compile time, and lazily built at first use.
See lazy_regex!
Features and Reexport
With default features, lazy-regex use the regex crate with its default features, tailored for performances and complete Unicode support.
You may enable a different set of regex features by directly enabling them when importing lazy-regex.
It's also possible to use the regex-lite crate instead of the regex crate by declaring the lite feature:
lazy-regex = { version = "3.0", default-features = false, features = ["lite"] }
The lite flavor comes with slightly lower performances and a reduced Unicode support (see crate documentation) but also a much smaller binary size.
If 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.
Canop/lazy-regex có 230 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/Canop/lazy-regex. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
Canop/lazy-regex có phải mã nguồn mở không?
Có — Canop/lazy-regex phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/Canop/lazy-regex.
Canop/lazy-regex còn đang phát triển không?
Commit gần nhất trên Canop/lazy-regex là 1 tháng trước (theo timestamp GitHub). Repo có 18 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Canop/lazy-regex là gì?
Canop/lazy-regex (Canop/lazy-regex) là dự án Rust trên GitHub. Theo mô tả gốc: lazy static regular expressions checked at compile time
Canop/lazy-regex so với các dự án Backend khác thế nào?
Canop/lazy-regex được TopGit xếp vào nhóm Backend, với 230 sao GitHub và viết bằng Rust. Xem trang chủ đề Backend trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
Đọc thêm về Canop/lazy-regex ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/Canop/lazy-regex là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về lazy-regex?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về lazy-regex.