jzelkowsz has asked for the wisdom of the Perl Monks concerning the following question:

I come here seeking wisdom and the solution to a problem. My desired result is:

DC=VIP-MAIN,DC=ORG

DC=VIPM,DC=ORG

DC=VIP-MAIN,DC=ORG

DC=FISC,DC=DS,DC=PPKS,DC=COM

DC=ZAPK,DC=PRIV

DC=MINISTERED,DC=NET

I am trying to remove all characters up to the first "DC=". I am not having a good result.

Current result:

DC=DC=ORG

DC=DC=ORG

DC=DC=ORG

DC=DC=COM

DC=DC=PRIV

DC=DC=NET

I have tried a couple different solution but have no good result. I beg for some wisdom, please.

#My code: while(<DATA>) { # s/^ou\=.+DC\=/DC\=/i; s/.+?(?=DC\=)/DC\=/i; print ; } __DATA__ OU=NEW HIRES,OU=PBK,DC=VIP-MAIN,DC=ORG OU=0543 - DR. SMITH PHYSICIANS\, INC,OU=549 VPIU-PARENT - DEPT MANAGED +,DC=VIPM,DC=ORG OU=SYMPH,OU=MANAGED,DC=VIP-MAIN,DC=ORG OU=USERS,OU=MANAGED,DC=FISC,DC=DS,DC=PPKS,DC=COM OU=USERS,OU=COVE PA,OU=PEOPLE,DC=ZAPK,DC=PRIV OU=USER,OU=MANAGED,DC=MINISTERED,DC=NET

Replies are listed 'Best First'.
Re: Regex for AD Organizational Units
by choroba (Cardinal) on Jul 10, 2019 at 19:49 UTC
    You were almost there!

    s/.+?(?=DC=)//;

    i.e. "remove the shortest string followed by DC=".

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Thank you, choroba! This is working very well.
Re: Regex for AD Organizational Units
by Discipulus (Canon) on Jul 10, 2019 at 19:58 UTC
    hello jzelkowsz,

    you now have your regex, but why not using a proper tool like Net::LDAP ? see also https://ldap.perl.org/

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Hi Discipulus: I am not working directly with Active Directory but with an Oracle report whose information I must verify. Maybe I can use Net::Ldap in the future.