#regex #split #substring #string #place #split-inclusive

regex-split

An implementation of split_inclusive for the regex crate

1 unstable release

0.1.0 Nov 9, 2022

#879 in Text processing

Download history 433/week @ 2024-11-16 427/week @ 2024-11-23 528/week @ 2024-11-30 922/week @ 2024-12-07 849/week @ 2024-12-14 438/week @ 2024-12-21 512/week @ 2024-12-28 1166/week @ 2025-01-04 739/week @ 2025-01-11 609/week @ 2025-01-18 542/week @ 2025-01-25 712/week @ 2025-02-01 653/week @ 2025-02-08 589/week @ 2025-02-15 810/week @ 2025-02-22 564/week @ 2025-03-01

2,687 downloads per month
Used in 3 crates

MIT/Apache

15KB
157 lines

regex-split

The regex crate doesn't provide split_inclusive, which is found in the standard library for string, etc. There's an unstable feature that allows a regex to be used as the search pattern for a split, yadda yadda, etc., but who wants to use unstable these days?

Anyway, this library adds split_inclusive and split_inclusive_left, with the difference being that split_inclusive_left places the delimiter at the beginning of the substring, where split_inclusive places it at the end.

Usage

First, add the package.

$ cargo add regex-split

Then import regex_split::RegexSplit wherever you'd like to use the extra methods. Consuming the new methods is straightforward.

use regex_split::RegexSplit;

// split_inclusive
let re = Regex::new("\r?\n").unwrap();
let text = "This is just\na set of lines\r\nwith different newlines.";
let v: Vec<&str> = re.split_inclusive(text).collect();

assert_eq!(v, [
    "This is just\n",
    "a set of lines\r\n",
    "with different newlines.",
]);

// split_inclusive_left
let re = Regex::new("(?m)^-").unwrap();
let text = "List of fruits:\n-apple\n-pear\n-banana";
let v: Vec<&str> = re.split_inclusive_left(text).collect();

assert_eq!(v, [
    "List of fruits:\n",
    "-apple\n",
    "-pear\n",
    "-banana",
]);

That's pretty much it.

Dependencies

~2–3MB
~53K SLoC