zoukankan      html  css  js  c++  java
  • Java正则表达式例子汇总

    1.过滤特殊字符

    package com.sheepmu.text;
      /*    
      * @author sheepmu
      */ 
    public class HWCompetition {
    	  public static void main(String[] args){
    		  String s="a%&a^b}b*[cc]#d{d"ee/ff\gg"; //!!!!!  "是为了在字符串中转义";  \ 只是为了在字符串中转义
    		  System.out.println("原串----->"+s);
    		  String regex="[%{}^\[\]*#\\/&"]";//在[ ]中,需要\来转义[];需要\来转义;来转义"
    		  String news=s.replaceAll(regex,"");
    		  System.out.println("过滤后的串----->"+news);
    	  }  
    }	 	 
    

    package com.sheepmu.text;
      /*    
      * @author sheepmu
      */ 
    public class HWCompetition {
    	  public static void main(String[] args){
    		  String s="a%&a^b}b*[cc]#d{d"ee/ff\gg"; //!!!!!  "是为了在字符串中转义";  \ 只是为了在字符串中转义
    		  System.out.println("原串----->"+s);
    		  String regex="[^%{}^\[\]*#\\/&"]";// 在[^-----------]的第一个位置出现^表示非!!!!!!!
    		  String news=s.replaceAll(regex,"");
    		  System.out.println("过滤掉  非 特殊字符的串----->"+news);
    	  }  
    }	 
    	 
     
     

    package com.sheepmu.text;
      /*    
       * StringReverse(char *strIn,char *output)找出 strIn 里面所有大写字母,将其逆序输出
          如输入:strIn:"abcHDLmnkKl",输出:output:"KLDH",
      * @author sheepmu
      */ 
    public class HWCompetition {
    	  public static void main(String[] args){
    		  String s="abcHDLmnkKl";  
    		  String news=s.replaceAll("[^A-Z]","");//去掉非大写剩下的就是大写了撒~~~
    		   StringBuffer sb=new StringBuffer(news);//String---->StringBuffer 
    		   String result=sb.reverse().toString();
    		  System.out.println("结果----->"+ result);//KLDH
    	  }  
    }	 
    	 
     
     


  • 相关阅读:
    训练20191009 2018-2019 ACM-ICPC, Asia East Continent Finals
    [学习笔记] 有上下界网络流
    [HDU4609] 3-idiots
    [HDU1402] A * B Problem Plus
    [HNOI2017] 礼物
    训练20191007 2017-2018 ACM-ICPC Latin American Regional Programming Contest
    [ZJOI2014] 力
    训练20191005 2017-2018 ACM-ICPC Asia East Continent League Final
    [一本通学习笔记] 树链剖分
    [一本通学习笔记] 快速幂
  • 原文地址:https://www.cnblogs.com/oversea201405/p/3766897.html
Copyright © 2011-2022 走看看