Announcing regex-lite: A Lightweight Regex Engine for Searching Strings

2023/07/05
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

The regex-lite crate provides a lightweight regex engine for searching strings. It offers a syntax that is nearly identical to the popular regex crate. However, regex-lite prioritizes smaller binary sizes and shorter Rust compile times over performance and functionality. As a result, regex searches in this crate may be slower compared to the regex crate. Additionally, regex-lite has limited Unicode support, only matching codepoint by codepoint.

To use regex-lite, simply add it as a dependency in your project's Cargo.toml file. Here's an example of how to use regex-lite in a Rust project:

  1. Create a new Rust project:
$ cargo new my_project
$ cd my_project

  1. Add regex-lite as a dependency:
# Cargo.toml
[dependencies]
regex-lite = "0.1"

  1. Edit src/main.rs with the following code:
use regex_lite::Regex;

fn main() {
    let haystack = "Hello, world!";
    let pattern = r"world";

    let re = Regex::new(pattern).unwrap();
    if let Some(captures) = re.captures(haystack) {
        let match_str = captures.get(0).unwrap().as_str();
        println!("Found match: {}", match_str);
    } else {
        println!("No match found");
    }
}

  1. Run the program:
$ cargo run

This example demonstrates a basic regex search using regex-lite. For more examples and detailed API documentation, refer to the official documentation.