Lines Matching +full:to +full:- +full:regex
1 regex chapter
4 syntax is similar to Perl-style regular expressions, but lacks a few features
6 linear time with respect to the size of the regular expression and search text.
10 [](https://github.com/rus…
11 [](https://crates.io/crates/regex)
12 [](https://github.com/rust…
16 [Module documentation with examples](https://docs.rs/regex).
22 [`Regex` type](https://docs.rs/regex/*/regex/struct.Regex.html).
26 To bring this crate into your repository, either add `regex` to your
27 `Cargo.toml`, or run `cargo add regex`.
29 Here's a simple example that matches a date in YYYY-MM-DD format and prints the
33 use regex::Regex;
36 let re = Regex::new(r"(?x)
38 -
40 -
43 let caps = re.captures("2010-03-14").unwrap();
51 If you have lots of dates in text that you'd like to iterate over, then it's
52 easy to adapt the above example with an iterator:
55 use regex::Regex;
58 On 2010-03-14, foo happened. On 2014-10-14, bar happened.
62 let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
65 // Note that all of the unwraps are actually OK for this regex
66 // because the only way for the regex to match is if all of the
83 ### Usage: Avoid compiling the same regex in a loop
85 It is an anti-pattern to compile the same regular expression in a loop since
87 to a few **milliseconds** depending on the size of the regex.) Not only is
89 allocations internally to the matching engines.
91 In Rust, it can sometimes be a pain to pass regular expressions around if
93 [`lazy_static`](https://crates.io/crates/lazy_static) crate to ensure that
99 use regex::Regex;
101 fn some_helper_function(text: &str) -> bool {
103 static ref RE: Regex = Regex::new("...").unwrap();
109 Specifically, in this example, the regex will be compiled when it is used for
114 The main API of this crate (`regex::Regex`) requires the caller to pass a
115 `&str` for searching. In Rust, an `&str` is required to be valid UTF-8, which
118 To match on arbitrary bytes, use the `regex::bytes::Regex` API. The API
119 is identical to the main API, except that it takes an `&[u8]` to search
121 `regex::bytes::Regex`, while `.` will match any *UTF-8 encoded Unicode scalar
124 This example shows how to find all null-terminated strings in a slice of bytes:
127 use regex::bytes::Regex;
129 let re = Regex::new(r"(?P<cstr>[^\x00]+)\x00").unwrap();
133 // The unwrap is OK here since a match requires the `cstr` capture to match.
142 using the main API, `[^\x00]+` would instead match any valid UTF-8 sequence
147 This demonstrates how to use a `RegexSet` to match multiple (possibly
151 use regex::RegexSet;
167 // You can also test whether a particular regex matched:
183 parser, abstract syntax and a high-level intermediate representation for
185 This may be useful if you're implementing your own regex engine or otherwise
186 need to do analysis on the syntax of a regular expression. It is otherwise not
189 [Documentation `regex-syntax`.](https://docs.rs/regex-syntax)
197 optimizations performed by this crate to disable.
201 necessary, then such a configuration is perfectly serviceable. To disable
205 [dependencies.regex]
207 default-features = false
208 # regex currently requires the standard library, you must re-enable it.
212 This will reduce the dependency tree of `regex` down to a single crate
213 (`regex-syntax`).
216 [in the "Crate features" section of the documentation](https://docs.rs/regex/*/#crate-features).
224 to use this crate can be increased in minor version updates. For example, if
225 regex 1.0 requires Rust 1.20.0, then regex 1.0.z for all values of `z` will
226 also require Rust 1.20.0 or newer. However, regex 1.y for `y > 0` may require a
229 In general, this crate will be conservative with respect to the minimum
237 * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
238 https://www.apache.org/licenses/LICENSE-2.0)
239 * MIT license ([LICENSE-MIT](LICENSE-MIT) or
244 The data in `regex-syntax/src/unicode_tables/` is licensed under the Unicode
246 ([LICENSE-UNICODE](https://www.unicode.org/copyright.html#License)).