do something with regex
Terkwood opened this issue · 0 comments
Terkwood commented
we need to be able to handle multiple regex hits in a file, each individually
create a proper data structure for this app
each file may have multiple matches of multiple searches
- we want grouped by path so that when a file is edited, we can re-match all regex against it
- use
std::path::Path
- integration test with file
- learn https://docs.rs/regex/latest/regex/struct.Regex.html#method.replace
matches expose start
and end
offsets
https://docs.rs/regex/latest/regex/struct.Match.html#method.start
this will help us form a file preview
multi matches
https://stackoverflow.com/a/58010784/9935916
use lazy_static::lazy_static;
use regex::Regex;
fn str_strip_numbers(s: &str) -> Vec<i64> {
lazy_static! {
static ref RE: Regex = Regex::new(r"\d+").unwrap();
}
// iterate over all matches
RE.find_iter(s)
// try to parse the string matches as i64 (inferred from fn type signature)
// and filter out the matches that can't be parsed (e.g. if there are too many digits to store in an i64).
.filter_map(|digits| digits.as_str().parse().ok())
// collect the results in to a Vec<i64> (inferred from fn type signature)
.collect()
}