我有一个用分隔符分隔的字符串.我想用正则表达式拆分这个字符串,并保留分隔符.
我目前的代码是:
use regex::Regex; // 1.1.8
fn main() {
let seperator = Regex::new(r"([ ,.]+)").expect("Invalid regex");
let splits: Vec<_> = seperator.split("this... is a, test").into_iter().collect();
for split in splits {
println!("\"{}\"", split);
}
}
其输出为:
"this"
"is"
"a"
"test"
我希望保留分隔符(在本例中为空格字符),我希望看到的输出是:
"this"
"... "
"is"
" "
"a"
", "
"test"
如果可能的话,我怎样才能用regex美元实现这样的行为呢?
这与Split a string keeping the separators不同,后者使用标准库,而不是regex crate .