java正则表达式提取数字、英文、中文

时间:2024-03-05 11:41:38

直接上代码吧


package net.qmgf.proj.wbmining.fut.service.impl;


import com.itextpdf.text.log.Logger;
import com.itextpdf.text.log.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 正则
 *
 * @author He
 * @date 2020/9/3 14:42
 * @return
 **/
public class RegTest {

    public static void main(String[] args) {

        String str = "……^1dsf  の  adS   DFASFSADF阿德斯防守对方asdfsadf37《?:?@%#¥%#¥%@#$%#@$%^><?1234";
        String regEx = "[a-zA-Z]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            sb.append(m.group());
        }
        System.out.println(sb);
    }
}

  1. 提取中文:regEx=“[\u4e00-\u9fa5]";
  2. 提取数字:regEx=“[0-9]";
  3. 提取英文:regEx=“[a-zA-Z]";
  4. 提取英文和数字:regEx=“[a-zA-Z0-9]";