日期:2014-05-20  浏览次数:20659 次

String split正则表达式的问题
高手给出个主意:  
4523425432NB,32423TY,4NT,43232423432YT,
对于以上的字符,最后两位是字母,前面是数字,位数不定,怎么把数字和字母分别取出.

------解决方案--------------------
public static void main(String[] args) {
  String str = "4523425432NB,32423TY,4NT,43232423432YT ";
  String[] strs = str.split( ", ");
  int length = strs.length;
  String[] number = new String[length];
  String[] character = new String[length];
  
  for(int i=0; i <length; i++) {
    number[i] = strs[i].substring(0, strs[i].length()-2);
    character[i] = strs[i].substring(strs[i].length()-2);
  }    
  for(int i=0; i <length; i++) {
    System.out.println(number[i] + " --> " + character[i]);
  }
}
------解决方案--------------------
高人
------解决方案--------------------
非贪婪匹配
(\\d+?)([a-zA-Z]{2})

group(1),group(2)
------解决方案--------------------
Java code
Pattern p = Pattern.compile("(\\d*)([A-Z]*),?");
String str = "4523425432NB,32423TY,4NT,43232423432YT";
Matcher m = p.matcher(str);
while (m.find()) {
    System.out.println(m.group(1)+"\t"+m.group(2));
}