#regex #posix #libc #part #api #posix-compliant #o-ses

regex-rs

Safe wrapper for [POSIX regular expressions API][regex-h] (provided by libc on POSIX-compliant OSes). Part of libnewsboat lib dependencies

1 unstable release

0.1.0 Dec 23, 2024

#29 in #libc

Download history 316/week @ 2024-12-23 265/week @ 2024-12-30

581 downloads per month
Used in 2 crates (via libnewsboat)

MIT license

4MB
669 lines

regex-rs (part of libnewsboat library)

Disclaimer: This project is part of Newsboat Rust libraries, I'm not it's author - merely maintaining up to date versions on Crates.io.

Description

Safe wrapper for [POSIX regular expressions API][regex-h] (provided by libc on POSIX-compliant OSes).


lib.rs:

Safe wrapper for POSIX regular expressions API (provided by libc on POSIX-compliant OSes).

use regex_rs::*;

let pattern = "This( often)? repeats time and again(, and again)*\\.";
let compilation_flags = CompFlags::EXTENDED;
let regex = Regex::new(pattern, compilation_flags)
    .expect("Failed to compile pattern as POSIX extended regular expression");

let input = "This repeats time and again, and again, and again.";
// We're only interested in the first match, i.e. the part of text
// that's matched by the whole regex
let max_matches = 1;
let match_flags = MatchFlags::empty();
let matches = regex
    .matches(input, max_matches, match_flags)
    .expect("Error matching input against regex");

// Found a match
assert_eq!(matches.len(), 1);

// Match spans from the beginning to the end of the input
assert_eq!(matches[0].start_pos, 0);
// `end_pos` holds one-past-the-end index
assert_eq!(matches[0].end_pos, input.len());

Dependencies

~14MB
~68K SLoC