Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUST

rust regex split

///	Note:
///	This section requires that this crate is compiled with the pattern
///	Cargo feature enabled, which requires nightly Rust.
///	
///	More information in the link below...
use regex::Regex; // 1.1.8

fn split_keep<'a>(r: &Regex, text: &'a str) -> Vec<&'a str> {
    let mut result = Vec::new();
    let mut last = 0;
    for (index, matched) in text.match_indices(r) {
        if last != index {
            result.push(&text[last..index]);
        }
        result.push(matched);
        last = index + matched.len();
    }
    if last < text.len() {
        result.push(&text[last..]);
    }
    result
}

fn main() {
    let seperator = Regex::new(r"([ ,.]+)").expect("Invalid regex");
    let splits = split_keep(&seperator, "this... is a, test");
    for split in splits {
        println!(""{}"", split);
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #rust #regex #split
ADD COMMENT
Topic
Name
4+4 =