How to check if a string is a palindrome ignoring case and non-alphanumeric characters in Rust

1 Answer

0 votes
use regex::Regex;

fn is_palindrome(s: &str) -> bool {
    // Create regex to remove non-alphanumeric characters
    let re = Regex::new(r"[^a-zA-Z0-9]").unwrap();
    let normalized = re.replace_all(s, "").to_lowercase();

    println!("{}", normalized);

    // Check if normalized string equals its reverse
    normalized == normalized.chars().rev().collect::<String>()
}

fn main() {
    let s = "+^-Ab#c!D 50...#  05*()dcB[]A##@!$";
    
    println!("{}", is_palindrome(s));
}



    
/*
run:

abcd5005dcba
true
   
*/
  
 

 



answered Aug 10, 2025 by avibootz
...