2017-03-16 137 views
3

是否有任何簡單的方法來拆分有只有字母(無空格)的排序字符串。拆分字符串,只有字母

例如,如果我有這樣的字符串:

string str ="aaasssdeettyy"; 

我需要拆分此字符串的子字符串:

string res = "aaa"; 
string res = "sss"; 
string res = "d"; 
string res = "ee"; 
string res = "tt"; 
string res = "yy"; 

有什麼辦法採用分體式()命令和正則表達式來實現它表達?

回答

0

試試這個:\1相同的文本匹配由第一組爲匹配(.)

Pattern compile = Pattern.compile("(.)\\1+"); 
Matcher matcher = compile.matcher("aaabbbcdddee"); 
while (matcher.find()) 
    System.out.println(matcher.group());