[code="java"]public static void main(String[] args) {
String s= "cn=测试用户1,ou=测试机构1,ou=湖北省,ou=org,o=essp,c=cn";
// String s= "cn=测试用户1,ou=测试一部,ou=测试机构1, ou=湖北省 ";
String cnPattern = "cn=(.*?)\s*,.*";
String ouPattern = "ou=([^, (org)]*)";
List<String> result = new ArrayList<String>();
Matcher cnMatcher = Pattern.compile(cnPattern).matcher(s);
if(cnMatcher.find()) {
result.add(cnMatcher.group(1));
}
Matcher ouMatcher = Pattern.compile(ouPattern).matcher(s);
int start = 0;
while(ouMatcher.find(start)) {
String ou = ouMatcher.group(1);
if(ou.length() > 0) {
result.add(ou);
}
start = ouMatcher.start() + 1;
}
StringBuilder sb = new StringBuilder();
for (int l = result.size(), i = l - 1; i >= 0; i--) {
String s1 = result.get(i);
if(i != l - 1) {
sb.append("-->");
}
sb.append(s1);
}
System.out.println(sb.toString());
}[/code]