Regex usage in velocity templates - removing the RDN of a distinguished name

I’m trying to remove the relative distinguished name from the AD distinguished name so that I can compare the container a user is in, to where they should be.

Originally I thought I could just use the split and use the comma as the parser. However I found that the RDN on some accounts have a comma within them such as CN=Howard, James,OU=corp,DC=example,DC=com, so I can’t parse using the comma.

How, or can I use regex and what would that look like?

I have tried something like: {"cn=[^,]+(?=,OU=)", "")) but I don’t know if velocity supports regex and if I can use this. I want to remove “CN=Howard, James,” but keep “OU=corp,DC=example,DC=com”.

Hi Fred,
I think you don’t need to really use regex - as it might be a bit complex. I’d suggest to make a substring based on index of “OU=” part. Something like that

String dn = "CN=Howard, James,OU=corp,DC=example,DC=com";
String domainPart = dn.substring(dn.indexOf("OU="));

And as Velocity allows you to execute Java methods it should be possible

Kamil,

Thanks, I was able to get it to work with the below logic.

"value": "#set($adSourceParts = $adDistinguishedName.split(\",OU=\"))#set($adSourceOU = $adSourceParts[1])#set($adSourceOU = 'OU=' + $adSourceOU)#set($adOuParts = $adou.split(\",\"))#set($idnOU = $adOuParts[0])#if(($lcs == 'active' || $lcs == 'onleave') && $adSourceOU != $idnOU)1#elseif($adSourceOU == $idnOU)0#{else}2#end"

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.