Crates.io | async-regex |
lib.rs | async-regex |
version | 0.1.1 |
created_at | 2025-09-12 18:54:24.214125+00 |
updated_at | 2025-09-12 19:23:46.266163+00 |
description | Empower regex with streaming capabilities - high-performance async streaming pattern search using regex for multi-byte pattern matching in data streams |
homepage | https://gitlab.com/Efimster/slib/-/tree/master/async-regex |
repository | https://gitlab.com/Efimster/slib |
max_upload_size | |
id | 1836064 |
size | 82,106 |
Empower regex with streaming capabilities!
A high-performance library that brings the power of regex pattern matching to streaming data. This crate extends the standard read_until
functionality to support multi-byte patterns using regex, making it perfect for parsing protocols, log files, and other structured data streams.
Why async-regex? This crate empowers regex with streaming capabilities - bringing the robust pattern matching of the
regex
crate to streaming data processing!
regex
crate for reliable pattern matchingunsafe
coderead_until
which only supports single bytesPerfect for:
use async_regex::read_until_pattern_async;
use futures::io::Cursor;
use tokio::runtime::Runtime;
let rt = Runtime::new().unwrap();
rt.block_on(async {
let mut reader = Cursor::new(b"HTTP/1.1 200 OK\r\nContent-Length: 42\r\n\r\n");
let mut buffer = Vec::new();
// Find HTTP status line using regex
let (matched, size) = read_until_pattern_async(
&mut reader,
r"HTTP/\d\.\d \d+",
&mut buffer
).await.unwrap();
assert_eq!(matched, b"HTTP/1.1 200");
assert_eq!(buffer, b"HTTP/1.1 200");
});
use async_regex::read_until_pattern_async;
use futures::io::Cursor;
use tokio::runtime::Runtime;
let rt = Runtime::new().unwrap();
rt.block_on(async {
let mut reader = Cursor::new(b"user@example.com and admin@company.org");
let mut buffer = Vec::new();
// Find email addresses using regex
let (matched, size) = read_until_pattern_async(
&mut reader,
r"\w+@\w+\.\w+",
&mut buffer
).await.unwrap();
assert_eq!(matched, b"user@example.com");
assert_eq!(buffer, b"user@example.com");
});
use async_regex::read_until_pattern;
use std::io::Cursor;
let mut reader = Cursor::new(b"2024-01-15 10:30:45 INFO: Application started");
let mut buffer = Vec::new();
// Find timestamp using regex
let (matched, size) = read_until_pattern(
&mut reader,
r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}",
&mut buffer
).unwrap();
assert_eq!(matched, b"2024-01-15 10:30:45");
assert_eq!(buffer, b"2024-01-15 10:30:45");
This crate is optimized for high-performance streaming pattern search with regex:
regex
crate for pattern matchingBenchmarks run on MacBook Pro (2019) with 8-Core Intel Core i9 @ 2.4GHz, 32GB RAM
This crate bridges the gap between regex and streaming data processing!
The Problem:
Our Solution:
Perfect for:
Use Case | Our Solution | regex crate | tokio::io::AsyncBufRead |
---|---|---|---|
Regex patterns on streaming data | โ Perfect! | โ In-memory only | โ Single-byte only |
Multi-byte pattern matching | โ Regex-powered | โ Full regex support | โ Single-byte only |
Streaming data processing | โ Memory efficient | โ Loads all data | โ Memory efficient |
Complex pattern matching | โ Full regex support | โ Full regex support | โ Single-byte only |
Async I/O | โ Native async | โ Sync only | โ Native async |
Large file processing | โ Streaming | โ Memory intensive | โ ๏ธ Limited patterns |
Protocol parsing | โ Perfect | โ Not suitable | โ ๏ธ Limited patterns |
๐ก Key Insight: This crate combines the power of regex with the efficiency of streaming, making it perfect for processing large files or continuous data streams with complex pattern matching requirements.
read_until_pattern_async<R>(reader: &mut R, pattern: &str, to: &mut Vec<u8>) -> Result<(Vec<u8>, usize)>
R: AsyncBufRead + Unpin
read_while_any_async<R>(reader: &mut R, check_set: &[u8], to: &mut Vec<u8>) -> Result<(u8, usize)>
R: AsyncBufRead + Unpin
read_until_pattern<R>(reader: &mut R, pattern: &str, to: &mut Vec<u8>) -> Result<(Vec<u8>, usize)>
R: BufRead
read_while_any<R>(reader: &mut R, check_set: &[u8], to: &mut Vec<u8>) -> Result<(u8, usize)>
R: BufRead
find_pattern(haystack: &[u8], needle: &Regex) -> Option<(usize, usize)>
Run tests:
cargo test
Run benchmarks:
cargo bench
Contributions are welcome! This crate aims to make regex pattern matching accessible for streaming data. Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License. See the LICENSE file for details.
async-regex empowers the powerful regex
crate with streaming capabilities, making it possible to use complex regex patterns on data streams without loading everything into memory. Perfect for protocol parsing, log processing, and any scenario where you need regex power on streaming data.