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

java正则表达式截取字符的问题
String sa=" temp1,abc b1,temp2 d1,defh,ghxs gh";
现在有个字符串,我想用正则表达式截取temp1,abc,temp2,defh,ghxs,哪位大神知道该怎么做啊
中间有逗号和空格符隔开,b1,d1,gh不要把截取出来
------解决方案--------------------
正则:

(?<=^
------解决方案--------------------
,)\s*([^,\s]+)



import java.util.regex.*;

public class TestTmp { 
    public static void main(String[] args) {
     String sa=" temp1,abc b1, temp2 d1,defh,ghxs gh";
        //(?<=^
------解决方案--------------------
,)\s*([^,\s]+)
        Pattern pattern = Pattern.compile("(?<=^
------解决方案--------------------
,)\\s*([^,\\s]+)");
        Matcher matcher = pattern.matcher(sa);
        int count = 0;
        while (matcher.find()) {
            System.out.println("第" + ++count + "项:【" + matcher.group(1) + "】");
        }
    }
}