Remove Emojis from String Rust

Viewed 359

How would I remove emojis from a string like "⚡hel✅lo"?

I know you'd need to make use of Regex and a few other stuff but I'm not sure how to write the syntax and replace everything in the string.

Thanks, help is really appreciated .

1 Answers

So I took some time to figure out, but here's the solution

/// Removes all emojis from a string **(retains chinese characters)**
///
/// # Arguments
///
/// * `string` - String with emojis
///
/// # Returns
///
/// * `String` - De-emojified string
///
/// # Examples
///
/// ```
///
/// // Remove all emojis from this string
/// let demojified_string = demoji(String::from("⚡hel✅lo"))
/// // Output: `hello`
/// ```
pub fn demoji(string: String) -> String {
    let regex = Regex::new(concat!(
        "[",
        "\u{01F600}-\u{01F64F}", // emoticons
        "\u{01F300}-\u{01F5FF}", // symbols & pictographs
        "\u{01F680}-\u{01F6FF}", // transport & map symbols
        "\u{01F1E0}-\u{01F1FF}", // flags (iOS)
        "\u{002702}-\u{0027B0}",
        "\u{0024C2}-\u{01F251}",
        "]+",
    ))
    .unwrap();

    regex.replace_all(&string, "").to_string()
}
Related