I have a string that is separated by a delimiter. I want to split this string using regex and keep the delimiters.
My current code is:
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);
}
}
The output of which is:
"this"
"is"
"a"
"test"
I would like to keep the separators (in this case the space characters), the output I would like to see is:
"this"
"... "
"is"
" "
"a"
", "
"test"
How can I, if at all possible, achieve such behavior with regex?
This is different from Split a string keeping the separators, which uses the standard library and not the regex crate.