我正在使用Spring Security 3.1 Active Directory。
我有一个AD结构,我需要从AD结构distinguishedName属性中获取OU值。
我想要的OU将始终是最后一个DC之后的第一个OU。
下面是我的代码示例 - 我需要获取myCompany OU值,必须有更好的方法来执行此操作:
公共课测试{
public static void main(String[] args) {
String s = "cn=harry,cn=group,ou=myCompany,ou=customers,dc=this,dc=that";
System.out.println("s: "+s);
String s1 = s.substring(s.indexOf("ou=") + 3);
System.out.println("s1: "+s1);
String s2 = s1.substring(s1.indexOf(""), s1.indexOf(",ou="));
System.out.println("s2: "+s2);
}
}
这输出以下内容:
s: cn=harry,ou=myCompany,ou=customers,dc=this,dc=that
s1: myCompany,ou=customers,dc=this,dc=that
s2: myCompany
有人可以帮忙吗?
答案 0 :(得分:1)
正则表达式!在下面(第一或第二)选择你的味道:
public static void main (String[] args){
String s = "cn=harry,cn=group,ou=users,ou=myCompany,ou=customers,dc=this,dc=that";
String first = s.replaceAll(".*?,ou=(.*?),.*", "$1");
String middle = s.replaceAll(".*?,ou=.*?,ou=(.*?),.*", "$1");
String third = s.replaceAll(".*,ou=(.*?),.*", "$1");
System.out.println(first);
System.out.println(middle);
System.out.println(third);
}