How to split a string with multiple delimiters in Rust

1 Answer

0 votes
use regex::Regex;

fn main() {
    let s = "aaa : bbb / ccc . ddd ? eee&fff - ggg_hhh | iii \n jjj";

    // Define the delimiters (comma, space, exclamation mark, question mark)
    let delimiters = Regex::new(r"[,\s!?\-|/\\\n._:&]+").unwrap();

    // Split the string using the defined delimiters
    let words: Vec<&str> = delimiters.split(s).collect();

    for value in words {
        println!("{}", value);
    }
}


 
      
/*
run:
   
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
iii
jjj
     
*/

 



answered Mar 4, 2025 by avibootz
...