Crate regex_split

source ·
Expand description

The trait RegexSplit adds a pair of split options to Regex: split_inclusive and split_inclusive_left. split_inclusive works similarly to the method of the same name in std, where split_inclusive_left includes the delimiter at the front of each substring instead of at the back.

split_inclusive

It’s possible to roll your own lines() function. Why? I don’t know, but you can.

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

This is useful if your delimiter includes some context that is associated with the substring to the right. Is that useful? No, not generally–but there’s really no substitute if you need it.

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",
]);

Use regex_split::bytes::RegexSplit for regex::bytes::Regex.

Modules

Structs

Yields all substrings delimited by a regular expression match inclusive of the match.
Yields all substrings delimited by a regular expression match inclusive of the match.

Traits