Java正则表达式去掉多余空格及英文标点符号


package test;

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

/* @author yb31,Edward
* Date 2005-3-4/2007-9-23
* contact http://hi.baidu.com/Edwardworld
*/
public class StringUtil {
public static void testReg(){
String stringInfo = "{infoNum='10' EdwardBlog='http://hi.baidu.com/Edwardworld' topicLength='20' titleShow='yes' EdwardMotto='I am a man,I am a true man!' /}";

System.out.println("待处理的字符串:" + stringInfo);

Pattern p=Pattern.compile("[.,\"\\?!:']");//增加对应的标点

Matcher m=p.matcher(stringInfo);

String first=m.replaceAll(""); //把英文标点符号替换成空,即去掉英文标点符号

System.out.println("去掉英文标点符号后的字符串:" + first);

p=Pattern.compile(" {2,}");//去除多余空格

m=p.matcher(first);

String second=m.replaceAll(" ");

System.out.println("去掉多余空格后的字符串" + second);//second为最终输出的字符串

}

public static void main(String [] args){
StringUtil.testReg();

}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM