正则表达式(可用于JAVA spring类 Mysql,orcal 等)

  一: 基本用法

  • \d:等同于字符组 [0-9],表示任意一个数字字符
  • \w:较为常见,等同于字符组[0-9a-zA-Z],表示任意一个world(单词字符)
  • \s:等同于[ \t\n\x0B\f\r],匹配的是一个空格字符(space)

  当然,它们也有相对应的大写形式,但是表示的意思却是截然相反的。

  • \D:等同于[^0-9],表示一个任意非数字字符
  • \W:等同于[^0-9a-zA-Z],表示任意一个非单词字符,往往会是一些特殊符号
  • \S:等同于[^\t\n\x0B\f\r],匹配一个任意非空格的字符
public static  void main(String[] args){
   String msg = "248jhf8ADFV89你好";

   String regxt= "\\w";
   String newMsg = msg.replaceAll(regxt," ");  // 

   System.out.println(newMsg);


   int numCount=0; //多少个数字
   int smallCount =0; //多少小写字母
   int bigCount =0;  //多少个大写字母
   int chinaCount = 0;  //多少个汉字

   int msgLength = msg.toCharArray().length;
   for (int i = 0; i< msgLength; i++) {

      char item = msg.toCharArray()[i];

      if(item>='0' && item <='9'){
         numCount++;

      }else if(item>='a' && item <='z'){
         smallCount++;

      }else if(item>='A' && item <='Z'){
         bigCount++;

      }else{
         chinaCount++;
      }



   }
   System.out.println(numCount);
   System.out.println(smallCount);
   System.out.println(bigCount);
   System.out.println(chinaCount);
   

}

执行结果如下:

             你好
6
3
4
2

Process finished with exit code 0

 

1.1 string split 的正则表达式

public  static void main(String[] args){

		String msg = "248jhf8ADFV89你好";

		String[] msgArray = msg.split("[0-9]");

		List<String> list = Arrays.asList(msgArray);

//		list.stream().forEach(
//				System.out::println
//		);

		list.stream().forEach(obj -> System.out.println(obj));


	}

 输出结果:

jhf
ADFV

你好

public static void main(String [] args){
    String str = "cyy,single.abc/https";
    String[] results = str.split("[,./]");
    for(String s : results){
        System.out.print(s+"  ");
    }
}
输出结果:cyy  single  abc  https

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值