C # regular expression to determine whether it is a special character

In fact, there are two ways to judge whether it is a special character:

The first idea is to list all special characters and determine that the target string contains special characters.

public bool IsSpecialChar(string str){
    Regex regExp = new Regex("[ \\[ \\] \\^ \\-_*×――(^)$%~!@@##$…&%¥—+=<>《》!!???::•`·、。,;,.;/\'\"{}()‘’“”-]");
    if(regExp.IsMatch(str)){
        return true;
    }
    return false;
}

The problem with this line of thinking is that you ca n’t miss any special character, or the code has a bug

The second idea is to list all characters that are not special characters and determine whether the target string does not contain these characters.

public bool IsSpecialChar(string str){
    Regex regExp = new Regex("[^0-9a-zA-Z\u4e00-\u9fa5]");
    if(regExp.IsMatch(str)){
        return true;
    }
    return false;
}

The advantage of this way of thinking is that as long as the Chinese characters, letters, numbers and other characters that are not special characters are listed.

Published 34 original articles · Like1 · Visits 1945

Guess you like

Origin blog.csdn.net/qq_38974638/article/details/104990807