str::rmatches¶
Level: reference · for working programmers
One line: matches from the right — the same matches, yielded last-to-first.
pub fn rmatches<P: Pattern>(&self, pat: P) -> RMatches<'_, P>
where
for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
Stable since 1.2.0.
Where matches could overlap, the two directions land on different offsets, because each greedily takes the first match it meets from its own end. "aaaaa" searched for "aa" gives two matches either way — at bytes 0 and 2 going forwards, at bytes 3 and 1 going backwards, with the leftover a at the opposite end.
The order is reversed, which surprises people more than the direction does: collecting straight into a Vec gives the matches back to front.
Use it when you want the last few of many matches without walking the whole string first.
Example¶
str_rmatches.rs in full — pasted here by tools/run_examples.py from the file CI compiles and runs.
fn main() {
let s = "one,two,three,four";
println!("{:?}", s.matches(',').collect::<Vec<&str>>());
println!("{:?}", s.rmatches(',').count());
// Order is reversed.
let words = "a1b22c333";
println!("{:?}", words.matches(char::is_numeric).collect::<Vec<&str>>());
println!("{:?}", words.rmatches(char::is_numeric).collect::<Vec<&str>>());
// Overlap resolution starts from the other end: the same number of
// matches, but at different offsets, and a different leftover 'a'.
println!("{:?}", "aaaaa".match_indices("aa").collect::<Vec<(usize, &str)>>());
println!("{:?}", "aaaaa".rmatch_indices("aa").collect::<Vec<(usize, &str)>>());
// The last two matches, without walking the whole string first.
println!("{:?}", s.rmatches(char::is_alphabetic).take(2).collect::<Vec<&str>>());
}
Verified output of str_rmatches.rs — regenerated by tools/run_examples.py, never hand-typed.
[",", ",", ","]
3
["1", "2", "2", "3", "3", "3"]
["3", "3", "3", "2", "2", "1"]
[(0, "aa"), (2, "aa")]
[(3, "aa"), (1, "aa")]
["r", "u"]
See also¶
str::matches— the forward directionstr::rmatch_indices— the same, with offsetsstr::rsplit— the gaps, from the back
str::rmatches in the standard library ↗
Po polsku¶
Najbardziej zaskakuje tu nie kierunek, lecz kolejność: zebrane prosto do wektora dopasowania wychodzą od końca, stąd ["3", "3", "3", "2", "2", "1"] zamiast lustrzanej kopii listy z matches. Ciekawsza jest linia z "aaaaa" — przy wzorcu, który mógłby na siebie nachodzić, każdy kierunek zachłannie bierze pierwsze trafienie od swojej strony, więc do przodu wypadają przesunięcia 0 i 2, a do tyłu 3 i 1: tyle samo dopasowań, w innych miejscach, z niewykorzystanym a po przeciwnej stronie. To zarazem odpowiedź na pytanie, po co osobna metoda, skoro istnieje .rev(): dla wzorca będącego łańcuchem znaków oba przebiegi mogą dać co innego, więc Matches nie jest wtedy iteratorem dwukierunkowym (DoubleEndedIterator) i .rev() po prostu się nie skompiluje. W praktyce sięga się po rmatches(...).take(2) — kilka ostatnich dopasowań bez przechodzenia całego tekstu.
Szukaj po polsku: iteracja od końca łańcucha znaków · nakładające się dopasowania · rust rmatches vs matches rev · rust DoubleEndedSearcher