Get the Chinese characters in the special string

    public static void main(String[] args) {
        //1. 可以在中括号内加上任何想要删除的字符,实际上是一个正则表达式
        String regExp="[\n`~!@#$%^&*()+-=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。, 、?]";

        //2. 这里是将特殊字符换为空字符串,""代表直接去掉
        String replace = "";

        //3. 要处理的字符串
        String str = "电单耗=[站电耗*站面积/(总面积-银行面积)+合计电]/[面积*0.2]";
        str = str.replaceAll(regExp,replace);
        System.out.println("去除特殊字符后的值:"+ str);

    }
Method 2:

public static void main(String[] args) { 
String str = " 123abd 测试 a  空格 bb字符串 "; 
System.out.println("过滤出汉字:" + str.replaceAll("\\s*","").replaceAll("[^(\\u4e00-\\u9fa5)]","")); }
Commonly used regular expressions:


Pure numbers: ^[0-9]*$
Numbers with n digits: ^\d{n}$
Numbers with at least n digits: ^\d{n,}$
Numbers with mn digits: ^\d{m,n }$

Numbers starting with zero and non-zero: ^(0|[1-9][0-9]*)$
Numbers with at most two decimal places at the beginning of non-zero: ^([1-9][0-9]* )(.[0-9]{1,2})?$
Positive or negative numbers with 1-2 decimal places: ^(\-)?\d+(\.\d{1,2})?$
Positive Numbers, negative numbers, and decimals: ^(\-|\+)?\d+(\.\d+)?$ Chinese
characters: ^[\u4e00-\u9fa5]{0,}$
English and numbers: ^[A-Za -z0-9]+$ or ^[A-Za-z0-9]{4,40}$
All characters with a length of 3-20: ^.{3,20}$
A string consisting of 26 English letters : ^[A-Za-z]+$
is a string composed of 26 uppercase English letters: ^[AZ]+$
is a string composed of 26 lowercase English letters: ^[az]+$
is composed of numbers and 26 characters A string of English letters: ^[A-Za-z0-9]+$ A string of
numbers, 26 English letters or underscores: ^\w+$ or ^\w{3,20}$ Chinese,
English 、Numbers include underscores: ^[\u4E00-\u9FA5A-Za-z0-9_]+$
 

Guess you like

Origin blog.csdn.net/weixin_55823910/article/details/127575652