字符串用正则替换

String s= "cn=测试用户1,ou=测试机构1,ou=湖北省,ou=org,o=essp,c=cn";
我想把上面的字符串用正则替换为:
湖北省->测试机构1->测试用户1

需求是:ou=org,o=essp,c=cn可以直接去掉(当然ou=org,o=essp,c=cn之间可能是有空格的)
但ou=xxx的数量是可变的,如字符串也可能是这样:cn=测试用户1,ou=测试一部,ou=测试机构1,ou=湖北省
需要变为:湖北省->测试机构1->测试一部->测试用户1。
希望平时正则用的多的同学帮一把,谢谢。

用java语法实现~

[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]

[b]我看哥们对正则理解还有误,你是不是想找ou=后面是不是,号且不能是org的字符串?[/b]
目前你的正则是
String ouPattern = "ou=([^, (org)]*)";
[b]它的意思为ou=后面不能是",号","空格","(","o","r","g",")"的任意字符.[/b]
[b]正则[]里面的字符都是当成单个字符,不能表示字符串.[/b]
String s= "cn=测试用户1,ou=测试机构1,[b]ou= 湖北省[/b],ou=org,o=essp,c=cn";
如果把ou=后面加上一个空格便无法匹配,如以上字声明字符串,
因为楼主说明过存在
(当然ou=org,o=essp,c=cn之间可能是有空格的)
再比如说在湖北省前面加上一个g也是不能匹配的
String s= "cn=测试用户1,ou=测试机构1,[b]ou=g湖北省[/b],ou=org,o=essp,c=cn";