JAVA中正则表达式
1.21.33,54.14,54.34
如上字符串我想提取所有数字 但是数字是用,或.分开的而且有数字开头的
本想用两次replaceAll函数
自己的思路是分两次把数字提取出来 补0
如 第一次取只有一个字符4 等补两个0
第二次去34 和54等 补一个0
如下
String result= "1.21.33,54.4,54.34. "
result=result.replaceAll( "(\\D)(\\d)(\\D) ", "$100$2$3 ");
result=result.replaceAll( "(\\D)(\\d\\d)(\\D) ", "$10$2$3 ");
但是不知道怎么匹配第一个数字1 而且我这么做结果也不正确,请各位前辈指教了
------解决方案--------------------给楼主实现了楼主的功能了。结贴吧
Pattern pattern = Pattern.compile( "\\w+[.,] ");
inputstr = "1.21.33,54.14,54.34. ";
String t = "0000 ";
String b = " ";
String[]a = pattern.split(inputstr);
matcher = pattern.matcher(inputstr);
while(matcher.find())
{
int start = matcher.start();
int end = matcher.end();
String s = inputstr.substring(start,end);
s = t.substring(0,4 - s.length())+s;
b +=s;
System.out.println(s);
}
System.out.println(b);
------解决方案--------------------http://community.csdn.net/Expert/TopicView3.asp?id=5597331
都是楼主的帖子,需求差不多,看看我的回帖,两次replaceAll,
没必要每次都靠大家来解决