Java正则表达式例子汇总

本文介绍了一种使用Java正则表达式过滤特殊字符的方法,并演示了如何反转字符串中的大写字母。通过具体示例代码展示了如何实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
	  }  
}	 
	 
 
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值