// include the latest version of the regex crate in your Cargo.toml
extern crate regex;
use regex::Regex;
fn main() {
let regex = Regex::new(r"(?uim)(^|(?:\p{L}+\P{L}+){1,3})(p[oó][zž][ií][cč][ií])($|(?:\P{L}+\p{L}+){1,3})").unwrap();
let string = "Ahoj,
potřebuji najít pozici slova v textu (a pak ukázat kousek textu před a po, resp víc slov hledám, ale pro demonstraci, stačí 1 slovo).
Mám tedy nějaká text, zkusím vyhledat třeba slovo „Richter“ (potřebuji aby to umělo s diakritikou i bez), takže jsem použil regexp a zjištuju kde je zase problém.
funkce strpos samozřejmě nefunguje dobře (díky multi-byte kódování ukáže jinou pozici než by měla)
funkce mb_strpos se zachová správně (kdo by to byl čekal ;-))
preg_match_all se chová stejně „hloupě“ jako strpos
našel jsem v komentářích starou funkci upravenou o „MB“ chování a ta funguje dobře (jako mb_strpos) více na php.net (https://www.php.net/…atch-all.php#…)
vyzkoušel jsem si matchAll z balíčku Strings a funguje stejně špatně jako preg_match_All
";
// result will be an iterator over tuples containing the start and end indices for each match in the string
let result = regex.captures_iter(string);
for mat in result {
println!("{:?}", mat);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Rust, please visit: https://docs.rs/regex/latest/regex/