Case-insensitive string matching in Rust
You can always convert both strings to the same casing. This will work for some cases:
let needle = "μτς";
let haystack = "ΜΤΣ";
let needle = needle.to_lowercase();
let haystack = haystack.to_lowercase();
for i in haystack.matches(&needle) {
println!("{:?}", i);
}
See also str::to_ascii_lowercase
for ASCII-only variants.
In other cases, the regex crate might do enough case-folding (potentially Unicode) for you:
use regex::RegexBuilder; // 1.4.3
fn main() {
let needle = "μτς";
let haystack = "ΜΤΣ";
let needle = RegexBuilder::new(needle)
.case_insensitive(true)
.build()
.expect("Invalid Regex");
for i in needle.find_iter(haystack) {
println!("{:?}", i);
}
}
However, remember that ultimately Rust's strings are UTF-8. Yes, you need to deal with all of UTF-8. This means that picking upper- or lower-case might change your results. Likewise, the only correct way to change text casing requires that you know the language of the text; it's not an inherent property of the bytes. Yes, you can have strings which contain emoji and other exciting things beyond the Basic Multilingual Plane.
See also:
- How can I case fold a string in Rust?
- Why is capitalizing the first letter of a string so convoluted in Rust?
If you're using the regex crate, you can make the pattern case insensitive:
let re = Regex::new("(?i)μτς").unwrap();
let mat = re.find("ΜΤΣ").unwrap();