| 1 |
regex-lite |
| 2 |
========== |
| 3 |
This crate provides a **lightweight** regex engine for searching strings. The |
| 4 |
regex syntax supported by this crate is nearly identical to what is found in |
| 5 |
the `regex` crate. Like the `regex` crate, all regex searches in this crate |
| 6 |
have worst case `O(m * n)` time complexity, where `m` is proportional to the |
| 7 |
size of the regex and `n` is proportional to the size of the string being |
| 8 |
searched. |
| 9 |
|
| 10 |
[](https://github.com/rust-lang/regex/actions) |
| 11 |
[](https://crates.io/crates/regex-lite) |
| 12 |
|
| 13 |
|
| 14 |
### Documentation |
| 15 |
|
| 16 |
https://docs.rs/regex-lite |
| 17 |
|
| 18 |
|
| 19 |
### Usage |
| 20 |
|
| 21 |
To bring this crate into your repository, either add `regex-lite` to your |
| 22 |
`Cargo.toml`, or run `cargo add regex-lite`. |
| 23 |
|
| 24 |
Here's a simple example that matches a date in YYYY-MM-DD format and prints the |
| 25 |
year, month and day: |
| 26 |
|
| 27 |
```rust |
| 28 |
use regex_lite::Regex; |
| 29 |
|
| 30 |
fn main() { |
| 31 |
let re = Regex::new(r"(?x) |
| 32 |
(?P<year>\d{4}) # the year |
| 33 |
- |
| 34 |
(?P<month>\d{2}) # the month |
| 35 |
- |
| 36 |
(?P<day>\d{2}) # the day |
| 37 |
").unwrap(); |
| 38 |
let caps = re.captures("2010-03-14").unwrap(); |
| 39 |
|
| 40 |
assert_eq!("2010", &caps["year"]); |
| 41 |
assert_eq!("03", &caps["month"]); |
| 42 |
assert_eq!("14", &caps["day"]); |
| 43 |
} |
| 44 |
``` |
| 45 |
|
| 46 |
If you have lots of dates in text that you'd like to iterate over, then it's |
| 47 |
easy to adapt the above example with an iterator: |
| 48 |
|
| 49 |
```rust |
| 50 |
use regex::Regex; |
| 51 |
|
| 52 |
const TO_SEARCH: &'static str = " |
| 53 |
On 2010-03-14, foo happened. On 2014-10-14, bar happened. |
| 54 |
"; |
| 55 |
|
| 56 |
fn main() { |
| 57 |
let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap(); |
| 58 |
|
| 59 |
for caps in re.captures_iter(TO_SEARCH) { |
| 60 |
// Note that all of the unwraps are actually OK for this regex |
| 61 |
// because the only way for the regex to match is if all of the |
| 62 |
// capture groups match. This is not true in general though! |
| 63 |
println!("year: {}, month: {}, day: {}", |
| 64 |
caps.get(1).unwrap().as_str(), |
| 65 |
caps.get(2).unwrap().as_str(), |
| 66 |
caps.get(3).unwrap().as_str()); |
| 67 |
} |
| 68 |
} |
| 69 |
``` |
| 70 |
|
| 71 |
This example outputs: |
| 72 |
|
| 73 |
```text |
| 74 |
year: 2010, month: 03, day: 14 |
| 75 |
year: 2014, month: 10, day: 14 |
| 76 |
``` |
| 77 |
|
| 78 |
|
| 79 |
### Minimum Rust version policy |
| 80 |
|
| 81 |
This crate's minimum supported `rustc` version is `1.65.0`. |
| 82 |
|
| 83 |
The policy is that the minimum Rust version required to use this crate can be |
| 84 |
increased in semver compatible updates. |
| 85 |
|
| 86 |
|
| 87 |
### Motivation |
| 88 |
|
| 89 |
The primary purpose of this crate is to provide an alternative regex engine |
| 90 |
for folks that are unhappy with the binary size and compilation time of the |
| 91 |
primary `regex` crate. The `regex-lite` crate does the absolute minimum possible |
| 92 |
to act as a drop-in replacement to the `regex` crate's `Regex` type. It avoids |
| 93 |
a lot of complexity by choosing not to optimize searches and to opt out of |
| 94 |
functionality such as robust Unicode support. By keeping the code simpler |
| 95 |
and smaller, we get binary sizes and compile times that are substantially |
| 96 |
better than even the `regex` crate with all of its features disabled. |
| 97 |
|
| 98 |
To make the benefits a bit more concrete, here are the results of one |
| 99 |
experiment I did. For `regex`, I disabled all features except for `std`: |
| 100 |
|
| 101 |
* `regex 1.7.3`: 1.41s compile time, 373KB relative size increase |
| 102 |
* `regex 1.8.1`: 1.46s compile time, 410KB relative size increase |
| 103 |
* `regex 1.9.0`: 1.93s compile time, 565KB relative size increase |
| 104 |
* `regex-lite 0.1.0`: 0.73s compile time, 94KB relative size increase |
| 105 |
|
| 106 |
The main reason why `regex-lite` does so much better than `regex` when all of |
| 107 |
`regex`'s features are disabled is because of irreducible complexity. There are |
| 108 |
certain parts of the code in `regex` that can't be arbitrarily divided based |
| 109 |
on binary size and compile time goals. It's instead more sustainable to just |
| 110 |
maintain an entirely separate crate. |
| 111 |
|
| 112 |
Ideas for improving the binary size and compile times of this crate even more |
| 113 |
are most welcome. |
| 114 |
|
| 115 |
|
| 116 |
### License |
| 117 |
|
| 118 |
This project is licensed under either of |
| 119 |
|
| 120 |
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 121 |
https://www.apache.org/licenses/LICENSE-2.0) |
| 122 |
* MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 123 |
https://opensource.org/licenses/MIT) |
| 124 |
|
| 125 |
at your option. |
| 126 |
|
| 127 |
The data in `regex-syntax/src/unicode_tables/` is licensed under the Unicode |
| 128 |
License Agreement |
| 129 |
([LICENSE-UNICODE](https://www.unicode.org/copyright.html#License)). |