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

regex-split

split_inclusive 对于 regex crate 的实现

1 个不稳定版本

0.1.0 2022年11月9日

814文本处理

Download history 340/week @ 2024-03-14 396/week @ 2024-03-21 458/week @ 2024-03-28 405/week @ 2024-04-04 416/week @ 2024-04-11 267/week @ 2024-04-18 239/week @ 2024-04-25 340/week @ 2024-05-02 294/week @ 2024-05-09 412/week @ 2024-05-16 300/week @ 2024-05-23 232/week @ 2024-05-30 278/week @ 2024-06-06 388/week @ 2024-06-13 232/week @ 2024-06-20 571/week @ 2024-06-27

1,507 每月下载量
topgrade 中使用

MIT/Apache

15KB
157

regex-split

regex crate 不提供 split_inclusive,这在标准库中的字符串等中存在。有一个不稳定的功能允许使用正则表达式作为 split 的搜索模式,等等,但谁还想用不稳定的功能呢?

无论如何,这个库添加了 split_inclusivesplit_inclusive_left,二者的区别在于 split_inclusive_left 将分隔符放在子字符串的开头,而 split_inclusive 将它放在末尾。

用法

首先,添加该包。

$ cargo add regex-split

然后,在您想要使用额外方法的地方导入 regex_split::RegexSplit。消耗新方法很简单。

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

就是这样。

依赖项

~2.1–3MB
~53K SLoC