日期:2014-05-20 浏览次数:20854 次
String str = "23578075;677449,9865333qweeqweq;(陈)6789,sdfas "; Matcher m = Pattern.compile("[,,;;()()](\\d{4})[,,;;()()]").matcher(str); while(m.find()) System.out.println(m.group(1));
------解决方案--------------------
用正则表达式应该很容易做到,下面是我的实验代码,请高手指教
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test3 { public static void main(String[] args) { String str = "23578075;677449,9865333,qweeqweq;(陈)6789,sdfas 1234"; String regex = "\\b\\d{4}\\b"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); while(m.find()) { System.out.println(m.group()); } } }