Case-insensitive string matching in Rust

前端 未结 2 865
隐瞒了意图╮
隐瞒了意图╮ 2021-01-12 01:36

Is there a simple way to use str::matches case-insensitively?

相关标签:
2条回答
  • 2021-01-12 02:08

    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);
    }
    

    In other cases, the regex crate might do enough case-folding for you:

    extern crate regex;
    
    use regex::RegexBuilder;
    
    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?
    0 讨论(0)
  • 2021-01-12 02:08

    If you're using the regex crate, you can make the pattern case insensitive:

    let re = Regex::new(r"(?i)μτς").unwrap();
    let mat = re.find("ΜΤΣ").unwrap();
    
    0 讨论(0)
提交回复
热议问题